C++ Win API - FindWindow() 或 EnumWindows() 来检索特定窗口 [英] C++ Win API - FindWindow() or EnumWindows() to retrieve specific windows

查看:32
本文介绍了C++ Win API - FindWindow() 或 EnumWindows() 来检索特定窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从特定窗口(标题和类名已知)检索窗口句柄时遇到以下问题:

I have the following problem with retrieving the window handle from a specific window (title and class name are known):

在两个不同的进程下有两个具有不同句柄的相同窗口,但是 FindWindow() 只能从最新生成的窗口中找到句柄,而不能从第一个窗口中找到句柄.

There are two identical windows with different handles under two different processes, but FindWindow() can find the handle only from the newest window spawned, never from the first one.

可以用什么代替?可以使用 EnumWindows() 检索具有相同特征的窗口列表吗?

What can be used instead? Can EnumWindows() be used to retrieve a list of windows with the same characteristics?

推荐答案

使用 Win32 API EnumWindows ,然后使用Win32 APIGetWindowThreadProcessId.

Use the Win32 API EnumWindows , and then check which process each window belongs to by using the Win32 API GetWindowThreadProcessId.

这是一个示例:

#include <iostream>
#include <Windows.h>
using namespace  std;
BOOL CALLBACK enumProc(HWND hwnd, LPARAM) {
    TCHAR buf[1024]{};

    GetClassName(hwnd, buf, 100);
    if (!lstrcmp(buf, L"Notepad"))
    {
        GetWindowText(hwnd, buf, 100);
        DWORD pid = 0;
        GetWindowThreadProcessId(hwnd, &pid);
        wcout << buf << " " << pid << endl;
    }
    return TRUE;
}

int main() {
    EnumWindows(&enumProc, 0);
}

如果需要查看每个进程的窗口,可以参考这个answer.

If you need to check the window of each process, you can refer to this answer.

这篇关于C++ Win API - FindWindow() 或 EnumWindows() 来检索特定窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆