// Locate the game's main window from inside the game process: the largest // visible, unowned top-level window the pid owns. Shared by the focus spoof // (subclass target) and the MKB forwarder (PostMessage target). #pragma once #include namespace coop::hook { inline HWND find_main_window(DWORD pid) { struct Ctx { DWORD pid; HWND best; long best_area; } ctx{pid, nullptr, 0}; EnumWindows( [](HWND hwnd, LPARAM lparam) -> BOOL { auto* c = reinterpret_cast(lparam); DWORD pid = 0; GetWindowThreadProcessId(hwnd, &pid); if (pid != c->pid || !IsWindowVisible(hwnd) || GetWindow(hwnd, GW_OWNER) != nullptr) { return TRUE; // not ours, hidden, or an owned dialog -- keep looking } RECT rect = {}; if (!GetWindowRect(hwnd, &rect)) { return TRUE; } const long area = (rect.right - rect.left) * (rect.bottom - rect.top); if (area > c->best_area) { c->best_area = area; c->best = hwnd; } return TRUE; }, reinterpret_cast(&ctx)); return ctx.best; } } // namespace coop::hook