欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

GetProcessMainWnd(获取进程窗口句柄)

程序员文章站 2022-07-13 22:32:49
...
typedef struct tagWNDINFO
{
		DWORD dwProcessId;
		HWND hWnd;
} WNDINFO, *LPWNDINFO;


BOOL CALLBACK YourEnumProc(HWND hWnd, LPARAM lParam)
{
		DWORD dwProcessId;
		GetWindowThreadProcessId(hWnd, &dwProcessId);
		LPWNDINFO pInfo = (LPWNDINFO)lParam;
		if( (dwProcessId == pInfo->dwProcessId))
		{
				pInfo->hWnd = hWnd;
				return FALSE;
		}
		return TRUE;
}

//获取进程窗口句柄
HWND GetProcessMainWnd(DWORD dwProcessId)
{
	WNDINFO wi={0};
	wi.dwProcessId = dwProcessId;
	EnumWindows(YourEnumProc,(LPARAM)&wi);
	HWND re_hwnd = wi.hWnd;
	while( GetParent(re_hwnd) != NULL )
	{
		re_hwnd = GetParent( re_hwnd );
	}

	return re_hwnd;
}