|
| 1 | +// Regression test for libnativeapi/nativeapi#53: |
| 2 | +// With TitleBarStyle::Hidden, WM_NCCALCSIZE for a maximized window must query |
| 3 | +// the monitor for the incoming proposed rect (MonitorFromRect(&proposed)) |
| 4 | +// rather than MonitorFromWindow(hwnd), which still points at the minimized |
| 5 | +// placeholder (-32000, -32000) while restoring from minimized on a secondary |
| 6 | +// monitor. |
| 7 | + |
| 8 | +#include <windows.h> |
| 9 | + |
| 10 | +#include <cstdio> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +#include "../src/window.h" |
| 14 | + |
| 15 | +namespace { |
| 16 | + |
| 17 | +int g_failures = 0; |
| 18 | + |
| 19 | +void Check(bool ok, const char* what) { |
| 20 | + if (!ok) { |
| 21 | + std::fprintf(stderr, "FAIL: %s\n", what); |
| 22 | + ++g_failures; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +void PumpFor(DWORD ms) { |
| 27 | + const ULONGLONG end = GetTickCount64() + ms; |
| 28 | + MSG msg = {}; |
| 29 | + while (true) { |
| 30 | + while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) { |
| 31 | + TranslateMessage(&msg); |
| 32 | + DispatchMessageW(&msg); |
| 33 | + } |
| 34 | + if (GetTickCount64() >= end) { |
| 35 | + break; |
| 36 | + } |
| 37 | + Sleep(5); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +RECT GetClientRectInScreen(HWND hwnd) { |
| 42 | + RECT client = {}; |
| 43 | + GetClientRect(hwnd, &client); |
| 44 | + POINT tl = {client.left, client.top}; |
| 45 | + POINT br = {client.right, client.bottom}; |
| 46 | + ClientToScreen(hwnd, &tl); |
| 47 | + ClientToScreen(hwnd, &br); |
| 48 | + return {tl.x, tl.y, br.x, br.y}; |
| 49 | +} |
| 50 | + |
| 51 | +bool RectsEqual(const RECT& a, const RECT& b) { |
| 52 | + return a.left == b.left && a.top == b.top && a.right == b.right && a.bottom == b.bottom; |
| 53 | +} |
| 54 | + |
| 55 | +LRESULT CALLBACK TestWndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { |
| 56 | + return DefWindowProcW(hwnd, msg, wp, lp); |
| 57 | +} |
| 58 | + |
| 59 | +} // namespace |
| 60 | + |
| 61 | +int main() { |
| 62 | + SetProcessDPIAware(); |
| 63 | + |
| 64 | + WNDCLASSW wc = {}; |
| 65 | + wc.lpfnWndProc = TestWndProc; |
| 66 | + wc.hInstance = GetModuleHandleW(nullptr); |
| 67 | + wc.lpszClassName = L"NativeAPIIssue53TestWindow"; |
| 68 | + RegisterClassW(&wc); |
| 69 | + |
| 70 | + std::vector<MONITORINFO> monitors; |
| 71 | + EnumDisplayMonitors( |
| 72 | + nullptr, nullptr, |
| 73 | + [](HMONITOR hmon, HDC, LPRECT, LPARAM data) -> BOOL { |
| 74 | + auto* list = reinterpret_cast<std::vector<MONITORINFO>*>(data); |
| 75 | + MONITORINFO info = {sizeof(info)}; |
| 76 | + if (GetMonitorInfoW(hmon, &info)) { |
| 77 | + list->push_back(info); |
| 78 | + } |
| 79 | + return TRUE; |
| 80 | + }, |
| 81 | + reinterpret_cast<LPARAM>(&monitors)); |
| 82 | + Check(!monitors.empty(), "at least one monitor present"); |
| 83 | + if (monitors.empty()) { |
| 84 | + return 1; |
| 85 | + } |
| 86 | + |
| 87 | + // Pick a secondary monitor if available; otherwise use the primary monitor. |
| 88 | + MONITORINFO target_monitor = monitors.front(); |
| 89 | + bool has_secondary = false; |
| 90 | + for (const auto& m : monitors) { |
| 91 | + if (!(m.dwFlags & MONITORINFOF_PRIMARY)) { |
| 92 | + target_monitor = m; |
| 93 | + has_secondary = true; |
| 94 | + break; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + HWND hwnd = CreateWindowExW(0, wc.lpszClassName, L"Issue53 Test", WS_OVERLAPPEDWINDOW, |
| 99 | + target_monitor.rcWork.left + 40, target_monitor.rcWork.top + 40, |
| 100 | + 640, 480, nullptr, nullptr, wc.hInstance, nullptr); |
| 101 | + Check(hwnd != nullptr, "create test window"); |
| 102 | + if (!hwnd) { |
| 103 | + return 1; |
| 104 | + } |
| 105 | + |
| 106 | + nativeapi::Window window(hwnd); |
| 107 | + window.SetTitleBarStyle(nativeapi::TitleBarStyle::Hidden); |
| 108 | + ShowWindow(hwnd, SW_SHOWMAXIMIZED); |
| 109 | + PumpFor(80); |
| 110 | + |
| 111 | + const RECT before_client = GetClientRectInScreen(hwnd); |
| 112 | + Check(RectsEqual(before_client, target_monitor.rcWork), |
| 113 | + "maximized hidden-title-bar client rect matches target monitor work area"); |
| 114 | + |
| 115 | + // Direct WM_NCCALCSIZE check with a proposed rect on each monitor while the |
| 116 | + // window is zoomed: rgrc[0] must resolve to the proposed rect's monitor. |
| 117 | + for (const auto& m : monitors) { |
| 118 | + NCCALCSIZE_PARAMS params = {}; |
| 119 | + params.rgrc[0] = {m.rcWork.left - 8, m.rcWork.top - 8, m.rcWork.right + 8, |
| 120 | + m.rcWork.bottom + 8}; |
| 121 | + SendMessageW(hwnd, WM_NCCALCSIZE, TRUE, reinterpret_cast<LPARAM>(¶ms)); |
| 122 | + Check(RectsEqual(params.rgrc[0], m.rcWork), |
| 123 | + "WM_NCCALCSIZE resolves monitor from proposed rect"); |
| 124 | + } |
| 125 | + |
| 126 | + // Minimize and restore: client rect and hit testing must stay on target_monitor. |
| 127 | + ShowWindow(hwnd, SW_MINIMIZE); |
| 128 | + PumpFor(80); |
| 129 | + ShowWindow(hwnd, SW_RESTORE); |
| 130 | + PumpFor(120); |
| 131 | + |
| 132 | + Check(IsZoomed(hwnd) != FALSE, "window returns to maximized state after restore"); |
| 133 | + const RECT after_client = GetClientRectInScreen(hwnd); |
| 134 | + Check(RectsEqual(after_client, target_monitor.rcWork), |
| 135 | + has_secondary |
| 136 | + ? "restored maximized window on secondary monitor keeps secondary work area" |
| 137 | + : "restored maximized window keeps monitor work area"); |
| 138 | + |
| 139 | + const int mid_x = (target_monitor.rcWork.left + target_monitor.rcWork.right) / 2; |
| 140 | + const int mid_y = (target_monitor.rcWork.top + target_monitor.rcWork.bottom) / 2; |
| 141 | + const LRESULT hit = SendMessageW(hwnd, WM_NCHITTEST, 0, MAKELPARAM(mid_x, mid_y)); |
| 142 | + Check(hit == HTCLIENT, "WM_NCHITTEST at center of restored window returns HTCLIENT"); |
| 143 | + |
| 144 | + DestroyWindow(hwnd); |
| 145 | + PumpFor(20); |
| 146 | + UnregisterClassW(wc.lpszClassName, wc.hInstance); |
| 147 | + |
| 148 | + if (g_failures == 0) { |
| 149 | + std::printf("PASS: window_hidden_titlebar_nccalcsize_windows_test (monitors=%zu)\n", |
| 150 | + monitors.size()); |
| 151 | + } |
| 152 | + return g_failures == 0 ? 0 : 1; |
| 153 | +} |
0 commit comments