14

プロセスIDがわかっている場合、アプリケーションのHWNDを取得するにはどうすればよいですか?誰でもサンプルを投稿できますか?MSV C ++2010を使用しています。Process::MainWindowHandleを見つけましたが、使用方法がわかりません。

4

4 に答える 4

25
HWND g_HWND=NULL;
BOOL CALLBACK EnumWindowsProcMy(HWND hwnd,LPARAM lParam)
{
    DWORD lpdwProcessId;
    GetWindowThreadProcessId(hwnd,&lpdwProcessId);
    if(lpdwProcessId==lParam)
    {
        g_HWND=hwnd;
        return FALSE;
    }
    return TRUE;
}
EnumWindows(EnumWindowsProcMy,m_ProcessId);
于 2013-12-22T15:32:28.723 に答える
3

このMSDNの記事で説明されているように、EnumWindows関数とGetWindowThreadProcessId()関数を使用できます。

于 2012-07-29T18:18:03.520 に答える
3

単一のPID(プロセスID)を複数のウィンドウ(HWND)に関連付けることができます。たとえば、アプリケーションが複数のウィンドウを使用している場合です。
次のコードは、特定のPIDごとにすべてのウィンドウのハンドルを検索します。

void GetAllWindowsFromProcessID(DWORD dwProcessID, std::vector <HWND> &vhWnds)
{
    // find all hWnds (vhWnds) associated with a process id (dwProcessID)
    HWND hCurWnd = NULL;
    do
    {
        hCurWnd = FindWindowEx(NULL, hCurWnd, NULL, NULL);
        DWORD dwProcID = 0;
        GetWindowThreadProcessId(hCurWnd, &dwProcID);
        if (dwProcID == dwProcessID)
        {
            vhWnds.push_back(hCurWnd);  // add the found hCurWnd to the vector
            wprintf(L"Found hWnd %d\n", hCurWnd);
        }
    }
    while (hCurWnd != NULL);
}
于 2018-01-11T12:56:27.560 に答える
2

Michael Haephratiのおかげで、最新のQt C++11のコードを少し修正しました。

#include <iostream>
#include "windows.h"
#include "tlhelp32.h"
#include "tchar.h"
#include "vector"
#include "string"

using namespace std;

void GetAllWindowsFromProcessID(DWORD dwProcessID, std::vector <HWND> &vhWnds)
{
    // find all hWnds (vhWnds) associated with a process id (dwProcessID)
    HWND hCurWnd = nullptr;
    do
    {
        hCurWnd = FindWindowEx(nullptr, hCurWnd, nullptr, nullptr);
        DWORD checkProcessID = 0;
        GetWindowThreadProcessId(hCurWnd, &checkProcessID);
        if (checkProcessID == dwProcessID)
        {
            vhWnds.push_back(hCurWnd);  // add the found hCurWnd to the vector
            //wprintf(L"Found hWnd %d\n", hCurWnd);
        }
    }
    while (hCurWnd != nullptr);
}
于 2019-07-13T09:43:52.190 に答える