带有enumwindows接口和进程的程序列表

时间:2017-09-08 12:32:55

标签: c++ interface window

我需要找到所有带有图形界面的开放窗口及其进程,我真的不知道该怎么做。我写了一些代码,但我刚成功找到了打开的窗口:

HWND hwnd = GetForegroundWindow(); // get handle of currently active window
GetWindowText(hwnd, wnd_title, sizeof(wnd_title));
cout << "Window with focus: " << wnd_title << endl << endl;

EnumWindows(EnumWindowsProc, 0); 

EnumWindowsProc定义如下:

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
    char class_name[80];
    char title[80];

    if (IsWindowVisible(hwnd)) {
        GetClassName(hwnd, class_name, sizeof(class_name));
        GetWindowText(hwnd, title, sizeof(title));
        cout << "Window title: " << title << endl;
        cout << "Class name: " << class_name << endl << endl;
    }   
    return TRUE;
}

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

我建议你不要因为

而检查IsWindowVisible
  

如果指定的窗口,其父窗口,其父窗口等具有 WS_VISIBLE 样式,则返回值为非零。否则,返回值为零   因为返回值指定窗口是否具有 WS_VISIBLE 样式,所以即使窗口被其他窗口完全遮挡,它也可能非零。

在枚举窗口时,您可以使用DWORD WINAPI GetWindowThreadProcessId(_In_ HWND hWnd, _Out_opt_ LPDWORD lpdwProcessId);来检索与该特定HWND相关的流程ID。

示例:

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
    char class_name[80];
    char title[80];
    DWORD dwProcessId;
    GetClassName(hwnd,class_name, sizeof(class_name));
    GetWindowText(hwnd,title,sizeof(title));

    // get process id based on hwnd
    GetWindowThreadProcessId(hwnd, &dwProcessId);

    std::cout << "Window title: "<< title << std::endl;
    std::cout << "Class name: "<< class_name << std::endl
    // display process id based on hwnd
    std::cout << "Process Id: " << dwProcessId  << std::endl;

    return TRUE;
}