c++ - 枚举子窗口

标签 c++ childwindow enumerate

几周前这里有人帮助我编写了一个枚举所有主窗口的类。

今天我试图修改那个类以枚举特定父窗口的所有子窗口。

这是头文件:

#include "TChar.h"
#include "string.h"
#include "windows.h"
#include "Winuser.h"
#include <string>
#include <vector>

using namespace std;

typedef std::basic_string<TCHAR> tstring;                   //define   basic_string<TCHAR> as a member of the std namespace 
                                                        //and at the same time use typedef to define the synonym tstring for it

class Handles {

public:
    struct child_data 
    {
        tstring caption;
        HWND handle;
    };


private:
    std::vector<child_data> stuff;                      //define a vector of objecttype "child_data" (the struct defined above) named stuff

    BOOL add_window(HWND hwnd) 
    {
        TCHAR String[200] = {0};
        if (!hwnd)
            return TRUE;                                // Not a window, return TRUE to Enumwindows in order to get the next handle
        if (!::IsWindowVisible(hwnd))
            return TRUE;                                // Not visible, return TRUE to Enumwindows in order to get the next handle 
        LRESULT result = SendMessageW(hwnd, WM_GETTEXT, sizeof(String), (LPARAM)String);        //result stores the number of characters copied from the window
        if (!result)
            return TRUE;                                // No window title, return TRUE to Enumwindows in order to get the next handle
        child_data data;                                // define an object of type child_data called data
        data.handle = hwnd;                             //copy the handle to the data.handle member

        for(int i = 0; i < result; i++)                 //copy each character to data.caption by using push_back
            data.caption.push_back(String[i]);

        stuff.push_back(data);                          //Put the whole data (with its members data.caption and data.handel) struct in the vector "stuff" using pushback
        return TRUE;
    }



    static BOOL CALLBACK EnumChildWindows(HWND hwnd, LPARAM lParam)
    {
        Handles* ptr = reinterpret_cast<Handles*>(lParam);
        return ptr->add_window(hwnd);
    }

public:
    Handles& enum_windows() 
    {
        stuff.clear();                                  //clean up
        if(!EnumWindows(EnumChildWindows, reinterpret_cast<LPARAM>(this))) 
        {
            // Error! Call GetLastError();
        }
        return *this;
    }

    std::vector<child_data>& get_results() 
    {
        return stuff;
    }
};

我通过以下方式调用该函数:

 std::vector<Handles::child_data> windows = Handles().enum_windows().get_results(); //Enumerate all visible windows and store handle and caption in "windows"

问题是:

我不太确定如何将父窗口的句柄传递给 header 中的回调函数。感觉我已经尝试了所有方法,但我总是以此类错误告终:变量 hwnd 未在 ... 中声明。

问题是我 100% 不懂这门课。我弄清楚的事情都有评论。

感谢您的帮助!

最佳答案

而不是调用 EnumWindows , 它枚举了屏幕上的所有顶级窗口,你可以调用 EnumChildWindows枚举给定父窗口的子窗口。为此,您可以将 enum_windows 的重载添加到您的 Handles 类:

Handles& enum_windows(HWND hParentWnd) 
{
    stuff.clear();                                  //clean up
    EnumChildWindows(hParentWnd, Handles_WndEnumProc, reinterpret_cast<LPARAM>(this));
    return *this;
}

EnumChildWindowsWNDENUMPROC 的错误名称。我建议将其重命名为更独特的名称,例如 Handles_WndEnumProc

关于c++ - 枚举子窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4843772/

相关文章:

qt - 如何将子窗口放入主窗口中(PyQt)

macos - 添加子窗口 :ordered: methods behaves strangely in snow leopard machine

python从特定索引继续循环

c++ - 哪些线程受 CPU 限制

python ctypes C++ 取回 char* 在 linux 上缺少最后一个字符

c++ - 如何降低 CPU 使用率?

cocoa - 允许 childWindow 中的 View 成为关键,而不会失去对 ParentWindow 的焦点

Objective-C:逐行读取文件

c++ - windows EnumProcesses 某些进程名称为 <unknown>

c++ - 具有相同名称的多个类导致 vtable 问题