Skip to content

Instantly share code, notes, and snippets.

@l2m2
Last active October 26, 2021 05:56
Show Gist options
  • Save l2m2/6fc1ebc6e304319240b00c08b8f81248 to your computer and use it in GitHub Desktop.
Save l2m2/6fc1ebc6e304319240b00c08b8f81248 to your computer and use it in GitHub Desktop.
获取当前进程的窗口句柄
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