Last active
October 26, 2021 05:56
-
-
Save l2m2/6fc1ebc6e304319240b00c08b8f81248 to your computer and use it in GitHub Desktop.
获取当前进程的窗口句柄
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct _TempHandleData { | |
unsigned long processId; | |
HWND windowHandle; | |
}; | |
BOOL isMainWindow(HWND handle) | |
{ | |
return ::GetWindow(handle, GW_OWNER) == (HWND)0 && ::IsWindowVisible(handle); | |
} | |
BOOL CALLBACK enumWindowsCallback(HWND handle, LPARAM lParam) | |
{ | |
_TempHandleData& data = *(_TempHandleData*)lParam; | |
unsigned long process_id = 0; | |
::GetWindowThreadProcessId(handle, &process_id); | |
if (data.processId != process_id || !isMainWindow(handle)) | |
return TRUE; | |
data.windowHandle = handle; | |
return FALSE; | |
} | |
HWND GetMainWindow(unsigned long process_id) | |
{ | |
_TempHandleData data; | |
data.processId = process_id; | |
data.windowHandle = 0; | |
::EnumWindows(enumWindowsCallback, (LPARAM)&data); | |
return data.windowHandle; | |
} | |
GetMainWindow(::GetCurrentProcessId()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment