Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;Win API-FindWindow()或EnumWindows()检索特定窗口_C++_C_Winapi - Fatal编程技术网

C++ C++;Win API-FindWindow()或EnumWindows()检索特定窗口

C++ C++;Win API-FindWindow()或EnumWindows()检索特定窗口,c++,c,winapi,C++,C,Winapi,我在从特定窗口检索窗口句柄时遇到以下问题(标题和类名已知): 在两个不同的进程下有两个具有不同句柄的相同窗口,但是FindWindow()只能从生成的最新窗口中找到句柄,而不能从第一个窗口中找到句柄 可以用什么来代替?能否使用EnumWindows()检索具有相同特征的窗口列表?使用Win32 API,然后使用Win32 API检查每个窗口属于哪个进程 以下是一个示例: #include <iostream> #include <Windows.h> using name

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

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

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

使用Win32 API,然后使用Win32 API检查每个窗口属于哪个进程

以下是一个示例:

#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);
}
#包括
#包括
使用名称空间std;
BOOL回调enumProc(HWND-HWND,LPARAM){
TCHAR buf[1024]{};
GetClassName(hwnd,buf,100);
如果(!lstrcmp(buf,L“记事本”))
{
GetWindowText(hwnd,buf,100);
DWORD pid=0;
GetWindowThreadProcessId(hwnd和pid);
wcout
typedef结构
{
常量字符*名称;
常量字符*类;
HWND手柄[10];
int handlesFound;
}搜索窗口信息;
搜索窗口信息;
wi.handlesFound=0;
wi.title=“WindowName”;
wi.class=“ClassName”;
布尔回调搜索窗口回调(HWND HWND,LPARAM LPARAM)
{
SearchWindoInfo*wi=(SearchWindoInfo*)LPRAM;
字符缓冲区[256];
如果(wi->handlesFound==10)
返回FALSE;
缓冲区[255]=0;
如果(wi->名称)
{
int rc=GetWindowText(hwnd,buffer,sizeof(buffer)-1);
if(rc)
{
if(strcmp(wi->name,buffer)==0)
{
wi->handles[wi->handlesFound++]=hwnd;
返回TRUE;
}
}
}
如果(wi->class)
{
int rc=GetClassName(hwnd,buffer,sizeof(buffer)-1);
if(rc)
{
if(strcmp(wi->class,buffer)==0)
{
wi->handles[wi->handlesFound++]=hwnd;
返回TRUE;
}
}
}
返回TRUE;
}
EnumWindows(searchWindowCallback,(LPRAM)和wi);
对于(int i=0;i
您好,如果此答案对您有帮助,请随时标记以帮助有相同问题的人,如果您有任何问题,请告诉我。谢谢。
typedef struct
{
    const char *name;
    const char *class;
    HWND handles[10];
    int handlesFound;
} SearchWindowInfo;

SearchWindowInfo wi;
wi.handlesFound = 0;
wi.title = "WindowName";
wi.class = "ClassName";

BOOL CALLBACK searchWindowCallback(HWND hwnd, LPARAM lParam)
{
    SearchWindoInfo *wi = (SearchWindoInfo *)lParam;
    char buffer[256];

    if (wi->handlesFound == 10)
        return FALSE;
    
    buffer[255] = 0;
    if (wi->name)
    {
        int rc = GetWindowText(hwnd, buffer, sizeof(buffer)-1);
        if(rc)
        {
            if (strcmp(wi->name, buffer) == 0)
            {
                wi->handles[wi->handlesFound++] = hwnd;
                return TRUE;
            }
        }
    }

    if (wi->class)
    {
        int rc = GetClassName (hwnd, buffer, sizeof(buffer)-1);
        if(rc)
        {
            if (strcmp(wi->class, buffer) == 0)
            {
                wi->handles[wi->handlesFound++] = hwnd;
                return TRUE;
            }
        }
    }

    return TRUE;
}

EnumWindows(searchWindowCallback, (LPARAM)&wi);
for(int i = 0; i < wi.handlesFound; i++)
{
    // yeah...
}