我檢查了.NET如何確定主窗口。
我的發(fā)現(xiàn)表明它也使用了EnumWindows()
。
此代碼應(yīng)該與.NET方式類似:
struct handle_data {
unsigned long process_id;
HWND window_handle;};HWND find_main_window(unsigned long process_id){
handle_data data;
data.process_id = process_id;
data.window_handle = 0;
EnumWindows(enum_windows_callback, (LPARAM)&data);
return data.window_handle;}BOOL CALLBACK enum_windows_callback(HWND handle, LPARAM lParam){
handle_data& data = *(handle_data*)lParam;
unsigned long process_id = 0;
GetWindowThreadProcessId(handle, &process_id);
if (data.process_id != process_id || !is_main_window(handle))
return TRUE;
data.window_handle = handle;
return FALSE; }BOOL is_main_window(HWND handle){
return GetWindow(handle, GW_OWNER) == (HWND)0 && IsWindowVisible(handle);}