【问题标题】:Filling a list with Window objects using EnumWindows使用 EnumWindows 用 Window 对象填充列表
【发布时间】:2016-01-16 06:23:08
【问题描述】:

我正在尝试用所有活动 Windows 的“Window”对象填充列表,但我无法找到将 Window 信息保存在我的 windowList 中的方法。windowList 通过 lParam 参数传递给回调函数.

这是我当前的代码:

std::list <Window> windowList; //in center.h


//in center.cpp:

void Center :: detectWindows()
{
    EnumWindows(detectWindowsProc, (LPARAM)&windowList);
}

BOOL CALLBACK detectWindowsProc(HWND hwnd, LPARAM inputList)
{
    if (IsWindow(hwnd) && IsWindowEnabled(hwnd) && IsWindowVisible(hwnd))
    {
        TCHAR TCharTitle[100];
        GetWindowText(hwnd, TCharTitle, 100);

        std::string title = convertTCharToStr(TCharTitle);

        Window * windowPtr;
        windowPtr = (Window*)inputList;

        Window newWindow((int)hwnd, title, true);
        *windowPtr = newWindow;

        std::cout << (int)hwnd << "  -  " << title << std::endl;
    }
    return true;
}

当我尝试通过 for 循环打印出 windowList 中的每个元素时,会弹出一条错误消息,提示“调试断言失败!表达式:列表迭代器不兼容”

这是for循环:

void Center::printWindowList()
{
    for (std::list<Window>::iterator it = windowList.begin(); it != windowList.end(); ++it)
        std::cout << ' ' << it->getTitle();
}

希望有人能帮忙

【问题讨论】:

    标签: c++ windows list pointers winapi


    【解决方案1】:

    在您的回调函数中,您将inputList 参数视为Window* 而不是list&lt;Window&gt;*。试试这个:

    BOOL CALLBACK detectWindowsProc(HWND hwnd, LPARAM inputList)
    {
        if (IsWindow(hwnd) && IsWindowEnabled(hwnd) && IsWindowVisible(hwnd))
        {
            TCHAR TCharTitle[100];
            GetWindowText(hwnd, TCharTitle, 100);
    
            std::string title = convertTCharToStr(TCharTitle);
    
            auto windowPtr = reinterpret_cast<std::list<Window>*>(inputList);
            windowPtr->push_back(Window((int)hwnd, title, true));
    
            std::cout << (int)hwnd << "  -  " << title << std::endl;
        }
        return true;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-06
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-21
      相关资源
      最近更新 更多