From cb2f41a92e6565d06574bfd00336068ae4d4bc48 Mon Sep 17 00:00:00 2001 From: LiJianying Date: Tue, 1 Dec 2026 08:02:22 +0800 Subject: [PATCH] Sync Windows fullscreen client size and preserve hidden-titlebar DWM borders --- src/platform/windows/window_windows.cpp | 172 ++++++++++-- tests/CMakeLists.txt | 9 + ...ullscreen_hidden_titlebar_windows_test.cpp | 247 ++++++++++++++++++ 3 files changed, 406 insertions(+), 22 deletions(-) create mode 100644 tests/window_fullscreen_hidden_titlebar_windows_test.cpp diff --git a/src/platform/windows/window_windows.cpp b/src/platform/windows/window_windows.cpp index 9e9dd43..5a15cf2 100644 --- a/src/platform/windows/window_windows.cpp +++ b/src/platform/windows/window_windows.cpp @@ -57,6 +57,12 @@ static const wchar_t* kHiddenFromTaskbarProperty = L"NativeAPIHiddenFromTaskbar" static const wchar_t* kNonFocusableProperty = L"NativeAPINonFocusable"; static const wchar_t* kNonActivatingProperty = L"NativeAPINonActivating"; static const wchar_t* kMouseInputPolicyProperty = L"NativeAPIMouseInputPolicy"; +// Set while DWM has detached non-client rendering (WM_DWMNCRENDERINGCHANGED with +// wParam == FALSE, such as during borderless fullscreen and until dwm.exe +// asynchronously re-attaches after leaving fullscreen). Unlike +// DwmGetWindowAttribute(DWMWA_NCRENDERING_ENABLED), which flips to TRUE the +// instant WS_CAPTION is restored in user mode, this tracks dwm.exe's actual state. +static const wchar_t* kDwmNcDisabledProperty = L"NativeAPIDwmNcDisabled"; struct MouseInputPolicy { bool forward = false; @@ -329,15 +335,17 @@ static LONG_PTR CaptionStyle(HWND hwnd) { return full == g_full_screen_windows.end() ? GetWindowLongPtrW(hwnd, GWL_STYLE) : full->second.style; } -static void SetCaptionStyle(HWND hwnd, LONG_PTR style) { +static void SetCaptionStyle(HWND hwnd, LONG_PTR style, bool notify_frame_changed = true) { constexpr LONG_PTR buttons = WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX; auto full = g_full_screen_windows.find(hwnd); if (full != g_full_screen_windows.end()) full->second.style = (full->second.style & ~buttons) | (style & buttons); const auto actual = GetWindowLongPtrW(hwnd, GWL_STYLE); SetWindowLongPtrW(hwnd, GWL_STYLE, (actual & ~buttons) | (style & buttons)); - SetWindowPos(hwnd, nullptr, 0, 0, 0, 0, - SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED); + if (notify_frame_changed) { + SetWindowPos(hwnd, nullptr, 0, 0, 0, 0, + SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED); + } } static bool NativeClosable(HWND hwnd) { if (g_full_screen_windows.count(hwnd)) { @@ -364,18 +372,23 @@ static ControlButtonPolicy* ControlPolicy(HWND hwnd, bool create) { } return policy; } -static void ApplyControlPolicy(HWND hwnd, const ControlButtonPolicy& policy) { +static void ApplyControlPolicy(HWND hwnd, const ControlButtonPolicy& policy, + bool notify_frame_changed = true) { auto style = CaptionStyle(hwnd); // The app-drawn caption still needs the system menu and MIN/MAX styles for // native commands and Windows 11 snap layouts while the title bar is hidden. const bool system_menu = policy.visible || GetPropW(hwnd, kTitleBarHiddenProperty); if (system_menu) style |= WS_SYSMENU; else style &= ~WS_SYSMENU; - SetCaptionStyle(hwnd, style); + SetCaptionStyle(hwnd, style, notify_frame_changed); if (HMENU menu = GetSystemMenu(hwnd, FALSE)) { EnableMenuItem(menu, SC_CLOSE, MF_BYCOMMAND | (policy.closable ? MF_ENABLED : (MF_DISABLED | MF_GRAYED))); - DrawMenuBar(hwnd); + // DrawMenuBar triggers classic non-client caption painting when DWM + // non-client rendering is temporarily off (e.g. leaving fullscreen); a + // hidden-title-bar window has no native caption bar to redraw. + if (!GetPropW(hwnd, kTitleBarHiddenProperty)) + DrawMenuBar(hwnd); } } @@ -579,6 +592,24 @@ static BOOL CALLBACK AttachTopEdgeChild(HWND child, LPARAM) { return TRUE; } +// Clears the non-client resize border (WindowRect - ClientRect) of a +// compositor-framed hidden-title-bar window to GDI black (BGRA 0x00000000, +// which DWM composites as transparent), removing any opaque Aero Basic border +// pixels painted while DWM non-client rendering was detached in fullscreen. +static void ClearHiddenTitleBarNonClientBorder(HWND hwnd) { + HDC wdc = GetWindowDC(hwnd); + if (!wdc) return; + RECT wr = {}, cr = {}; + if (GetWindowRect(hwnd, &wr) && GetClientRect(hwnd, &cr)) { + MapWindowPoints(hwnd, nullptr, reinterpret_cast(&cr), 2); + OffsetRect(&cr, -wr.left, -wr.top); + OffsetRect(&wr, -wr.left, -wr.top); + ExcludeClipRect(wdc, cr.left, cr.top, cr.right, cr.bottom); + FillRect(wdc, &wr, static_cast(GetStockObject(BLACK_BRUSH))); + } + ReleaseDC(hwnd, wdc); +} + static std::optional HandleHiddenTitleBarFrame(HWND hwnd, UINT message, WPARAM wp, LPARAM lp) { if (message == WM_PARENTNOTIFY && LOWORD(wp) == WM_CREATE) { @@ -626,13 +657,60 @@ static std::optional HandleHiddenTitleBarFrame(HWND hwnd, UINT message, const LRESULT edge = HiddenFrameResizeHit(hwnd, lp); return edge != 0 ? edge : hit; } - // Without the compositor's frame, DefWindowProc paints the classic frame and caption - // that the window style still asks for, straight over the content at the window's - // edges: on every step of a resize (WM_NCPAINT), on activation (WM_NCACTIVATE, and the - // undocumented WM_NCUAHDRAWCAPTION / WM_NCUAHDRAWFRAME the theme sends) and when the - // title or icon changes. The content repaints over it a moment later, so it flickers. - // There is no frame to paint, so none of that is let through. - if (!frameless) return std::nullopt; + // Never let legacy UxTheme paint a classic Win7-style caption or frame over a + // hidden-title-bar window (e.g. during style/placement transitions such as + // leaving borderless fullscreen before DWM re-composes the non-client area). + constexpr UINT kNcUahDrawCaption = 0x00AE; + constexpr UINT kNcUahDrawFrame = 0x00AF; + if (message == kNcUahDrawCaption || message == kNcUahDrawFrame) + return 0; + // When the compositor owns the frame (!frameless), never alter WM_NCPAINT's + // wParam (passing a custom HRGN disables DWM non-client rendering) and leave + // messages untouched whenever dwm.exe is actively compositing. Only while + // dwm.exe is detached (between WM_DWMNCRENDERINGCHANGED(FALSE) in fullscreen + // and WM_DWMNCRENDERINGCHANGED(TRUE) after leaving fullscreen), preserve the + // top client band across DefSubclassProc(WM_NCPAINT) and clear the resize + // borders back to transparent black so neither the top classic caption nor + // the left/right/bottom classic border ever appears. + if (!frameless) { + if (!GetPropW(hwnd, kDwmNcDisabledProperty)) + return std::nullopt; + if (message == WM_NCPAINT) { + RECT wr = {}, cr = {}; + GetWindowRect(hwnd, &wr); + GetClientRect(hwnd, &cr); + MapWindowPoints(hwnd, nullptr, reinterpret_cast(&cr), 2); + const int cw = cr.right - cr.left; + const int ch = cr.bottom - cr.top; + const int band_h = ch < 80 ? ch : 80; + const int ox = cr.left - wr.left; + const int oy = cr.top - wr.top; + HDC wdc = (cw > 0 && band_h > 0) ? GetWindowDC(hwnd) : nullptr; + HDC mem = wdc ? CreateCompatibleDC(wdc) : nullptr; + HBITMAP saved = (wdc && mem) ? CreateCompatibleBitmap(wdc, cw, band_h) : nullptr; + HGDIOBJ old = saved ? SelectObject(mem, saved) : nullptr; + if (saved) + BitBlt(mem, 0, 0, cw, band_h, wdc, ox, oy, SRCCOPY); + if (wdc) + ReleaseDC(hwnd, wdc); + + const LRESULT r = DefSubclassProc(hwnd, message, wp, lp); + + if (saved) { + wdc = GetWindowDC(hwnd); + if (wdc) { + BitBlt(wdc, ox, oy, cw, band_h, mem, 0, 0, SRCCOPY); + ReleaseDC(hwnd, wdc); + } + SelectObject(mem, old); + DeleteObject(saved); + } + if (mem) + DeleteDC(mem); + ClearHiddenTitleBarNonClientBorder(hwnd); + return r; + } + } if (message == WM_WINDOWPOSCHANGING && lp) { auto* pos = reinterpret_cast(lp); if (!(pos->flags & SWP_NOMOVE)) { @@ -643,9 +721,7 @@ static std::optional HandleHiddenTitleBarFrame(HWND hwnd, UINT message, } return std::nullopt; } - constexpr UINT kNcUahDrawCaption = 0x00AE; - constexpr UINT kNcUahDrawFrame = 0x00AF; - if (message == WM_NCPAINT || message == kNcUahDrawCaption || message == kNcUahDrawFrame) + if (message == WM_NCPAINT) return 0; // lParam -1 keeps the activation handling but skips the repaint. if (message == WM_NCACTIVATE) return DefSubclassProc(hwnd, message, wp, -1); @@ -823,6 +899,7 @@ static LRESULT WindowLifetimeMessage(HWND hwnd, UINT message, WPARAM wp, LPARAM RemovePropW(hwnd, kCornerPreferenceProperty); RemovePropW(hwnd, kBackgroundColorProperty); RemovePropW(hwnd, kTranslucentBackgroundProperty); + RemovePropW(hwnd, kDwmNcDisabledProperty); g_full_screen_windows.erase(hwnd); const auto result = DefSubclassProc(hwnd, message, wp, lp); WindowRegistry::GetInstance().Remove(static_cast(reference)); @@ -867,13 +944,26 @@ static LRESULT WindowLifetimeMessage(HWND hwnd, UINT message, WPARAM wp, LPARAM return result; } #ifndef NATIVEAPI_ENABLE_WINUI3 + constexpr UINT kWmDwmNcRenderingChanged = 0x031F; + if (message == kWmDwmNcRenderingChanged) { + if (wp) + RemovePropW(hwnd, kDwmNcDisabledProperty); + else + SetPropW(hwnd, kDwmNcDisabledProperty, reinterpret_cast(1)); + const LRESULT result = DefSubclassProc(hwnd, message, wp, lp); + if (wp && GetPropW(hwnd, kTitleBarHiddenProperty) && + !GetPropW(hwnd, kFramelessClientProperty)) { + ClearHiddenTitleBarNonClientBorder(hwnd); + } + return result; + } if (auto handled = HandleMaximizeButtonArea(hwnd, message, wp, lp)) return *handled; if (message == WM_PARENTNOTIFY && LOWORD(wp) == WM_CREATE && GetPropW(hwnd, kMaximizeButtonProperty)) AttachTopEdgeChild(reinterpret_cast(lp), 0); if (message == WM_NCCALCSIZE || message == WM_NCHITTEST || message == WM_PARENTNOTIFY || - message == WM_NCACTIVATE || message == WM_NCPAINT || message == 0x00AE || - message == 0x00AF || message == WM_SETTEXT || message == WM_SETICON) { + message == WM_WINDOWPOSCHANGING || message == WM_NCACTIVATE || message == WM_NCPAINT || + message == 0x00AE || message == 0x00AF || message == WM_SETTEXT || message == WM_SETICON) { if (auto handled = HandleHiddenTitleBarFrame(hwnd, message, wp, lp)) return *handled; } #endif @@ -904,7 +994,16 @@ static bool TrackWindowLifetime(HWND hwnd, WindowId id) { reinterpret_cast(&WindowLifetimeProc), &existing); const bool installed = SetWindowSubclass( hwnd, WindowLifetimeProc, reinterpret_cast(&WindowLifetimeProc), id) != FALSE; - return installed && !tracked; + if (installed && !tracked) { + BOOL nc_enabled = FALSE; + if (!SUCCEEDED(DwmGetWindowAttribute(hwnd, DWMWA_NCRENDERING_ENABLED, &nc_enabled, + sizeof(nc_enabled))) || + !nc_enabled) { + SetPropW(hwnd, kDwmNcDisabledProperty, reinterpret_cast(1)); + } + return true; + } + return false; } // Forward declaration @@ -1378,6 +1477,25 @@ bool Window::IsMinimized() const { return IsIconic(pimpl_->hwnd_) != FALSE; } +// Ensures host windows (such as Flutter's Win32Window hosting FLUTTERVIEW) +// resize their child surface to the final post-frame-change client rect and +// repaint immediately after entering or leaving fullscreen, without waiting +// for a focus change (Alt+Tab). +static void SyncFullScreenClientArea(HWND hwnd) { + if (!hwnd || !IsWindow(hwnd) || IsIconic(hwnd)) + return; + RECT client = {}; + if (GetClientRect(hwnd, &client)) { + const WPARAM size_type = IsZoomed(hwnd) ? SIZE_MAXIMIZED : SIZE_RESTORED; + SendMessageW(hwnd, WM_SIZE, size_type, + MAKELPARAM(client.right - client.left, client.bottom - client.top)); + } + UINT flags = RDW_INVALIDATE | RDW_NOERASE | RDW_ALLCHILDREN | RDW_UPDATENOW; + if (!GetPropW(hwnd, kTitleBarHiddenProperty)) + flags |= RDW_FRAME; + RedrawWindow(hwnd, nullptr, nullptr, flags); +} + void Window::SetFullScreen(bool is_full_screen) { HWND owner_window = pimpl_->hwnd_; if (owner_window && GetWindowThreadProcessId(owner_window, nullptr) != GetCurrentThreadId()) { @@ -1415,7 +1533,8 @@ void Window::SetFullScreen(bool is_full_screen) { SetWindowPos(hwnd, nullptr, monitor.rcMonitor.left, monitor.rcMonitor.top, monitor.rcMonitor.right - monitor.rcMonitor.left, monitor.rcMonitor.bottom - monitor.rcMonitor.top, - SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED); + SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_FRAMECHANGED); + SyncFullScreenClientArea(hwnd); NotifyWindowFullScreenChanged(hwnd, true); return; } @@ -1433,10 +1552,19 @@ void Window::SetFullScreen(bool is_full_screen) { SetWindowLongPtrW( hwnd, GWL_EXSTYLE, GetWindowLongPtrW(hwnd, GWL_EXSTYLE) | (state.ex_style & kFullScreenRemovedExStyle)); + // Restore window placement BEFORE sending SWP_FRAMECHANGED: while the window + // still covers monitor.rcMonitor, DWM keeps DWMWA_NCRENDERING_ENABLED == FALSE, + // so an early SWP_FRAMECHANGED at fullscreen bounds would force non-client + // repainting before leaving fullscreen. SetWindowPlacement(hwnd, &state.placement); + if (auto* policy = ControlPolicy(hwnd, false)) + ApplyControlPolicy(hwnd, *policy, /*notify_frame_changed=*/false); + UpdateFrameExtent(hwnd); + UpdateFrameRendering(hwnd, false); SetWindowPos(hwnd, nullptr, 0, 0, 0, 0, - SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED); - if (auto* policy = ControlPolicy(hwnd, false)) ApplyControlPolicy(hwnd, *policy); + SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE | + SWP_NOCOPYBITS | SWP_FRAMECHANGED); + SyncFullScreenClientArea(hwnd); NotifyWindowFullScreenChanged(hwnd, false); } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b6babd2..33f0966 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -690,3 +690,12 @@ if(APPLE AND NOT CMAKE_SYSTEM_NAME STREQUAL "iOS") add_test(NAME message_dialog_macos_sheet_test COMMAND message_dialog_macos_sheet_test) set_tests_properties(message_dialog_macos_sheet_test PROPERTIES TIMEOUT 15) endif() + +if(WIN32) + add_executable(window_fullscreen_hidden_titlebar_windows_test + window_fullscreen_hidden_titlebar_windows_test.cpp) + target_link_libraries(window_fullscreen_hidden_titlebar_windows_test PRIVATE nativeapi) + add_test(NAME window_fullscreen_hidden_titlebar_windows_test + COMMAND window_fullscreen_hidden_titlebar_windows_test) + set_tests_properties(window_fullscreen_hidden_titlebar_windows_test PROPERTIES TIMEOUT 20) +endif() \ No newline at end of file diff --git a/tests/window_fullscreen_hidden_titlebar_windows_test.cpp b/tests/window_fullscreen_hidden_titlebar_windows_test.cpp new file mode 100644 index 0000000..b818463 --- /dev/null +++ b/tests/window_fullscreen_hidden_titlebar_windows_test.cpp @@ -0,0 +1,247 @@ +// Regression test for libnativeapi/nativeapi#60: +// Entering and exiting fullscreen on Windows with TitleBarStyle::Hidden must: +// 1. Deliver WM_SIZE with the final post-frame-change client rect so child +// surfaces (e.g. Flutter's FLUTTERVIEW) resize immediately without waiting +// for focus change. +// 2. Never paint a classic Aero Basic / Win7 title bar strip over the top of +// ClientRect during WM_NCPAINT (wParam == 1) while dwm.exe asynchronously +// re-attaches non-client rendering after leaving fullscreen. +#include +#include +#include +#include +#include "nativeapi.h" + +#pragma comment(lib, "comctl32.lib") +#pragma comment(lib, "dwmapi.lib") + +namespace { +constexpr COLORREF kClientGreen = RGB(0, 200, 0); +int failures = 0; +HWND g_child = nullptr; +HBRUSH g_green_brush = nullptr; +int g_size_count = 0; +int g_last_size_w = 0; +int g_last_size_h = 0; +bool g_watching_top_band = false; +bool g_top_band_polluted = false; + +void Check(bool ok, const char* label) { + std::cout << (ok ? "PASS " : "FAIL ") << label << std::endl; + failures += !ok; +} + +// Outer subclass installed above WindowLifetimeProc so we sample the top client +// band immediately after WindowLifetimeProc finishes handling each WM_NCPAINT. +LRESULT CALLBACK OuterWatchProc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp, UINT_PTR id, + DWORD_PTR) { + if (message == WM_NCDESTROY) { + RemoveWindowSubclass(hwnd, OuterWatchProc, id); + return DefSubclassProc(hwnd, message, wp, lp); + } + const LRESULT r = DefSubclassProc(hwnd, message, wp, lp); + if (message == WM_NCPAINT && g_watching_top_band) { + HDC wdc = GetWindowDC(hwnd); + if (wdc) { + const COLORREF pixel = GetPixel(wdc, 40, 15); + ReleaseDC(hwnd, wdc); + if (pixel != CLR_INVALID && pixel != kClientGreen) + g_top_band_polluted = true; + } + } + return r; +} + +LRESULT CALLBACK HostProc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp) { + if (message == WM_ERASEBKGND) { + RECT rc = {}; + GetClientRect(hwnd, &rc); + FillRect(reinterpret_cast(wp), &rc, g_green_brush); + return 1; + } + if (message == WM_PAINT) { + PAINTSTRUCT ps = {}; + HDC hdc = BeginPaint(hwnd, &ps); + FillRect(hdc, &ps.rcPaint, g_green_brush); + EndPaint(hwnd, &ps); + return 0; + } + if (message == WM_SIZE) { + ++g_size_count; + g_last_size_w = LOWORD(lp); + g_last_size_h = HIWORD(lp); + RECT rect = {}; + if (g_child && GetClientRect(hwnd, &rect)) { + MoveWindow(g_child, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, + TRUE); + } + return 0; + } + return DefWindowProcW(hwnd, message, wp, lp); +} + +void Pump() { + MSG msg; + while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) { + TranslateMessage(&msg); + DispatchMessageW(&msg); + } +} + +bool ChildMatchesClient(HWND parent, HWND child) { + RECT pc = {}, cc = {}; + if (!GetClientRect(parent, &pc) || !GetClientRect(child, &cc)) + return false; + return (pc.right - pc.left) == (cc.right - cc.left) && + (pc.bottom - pc.top) == (cc.bottom - cc.top); +} + +bool HiddenTitleBarTopAligned(HWND hwnd) { + RECT wr = {}; + POINT origin = {0, 0}; + if (!GetWindowRect(hwnd, &wr) || !ClientToScreen(hwnd, &origin)) + return false; + return origin.y == wr.top; +} +} // namespace + +int main() { + g_green_brush = CreateSolidBrush(kClientGreen); + WNDCLASSW cls = {}; + cls.lpfnWndProc = HostProc; + cls.hInstance = GetModuleHandleW(nullptr); + cls.lpszClassName = L"NativeAPIFullScreenHiddenTitleBarTest"; + RegisterClassW(&cls); + + HWND hwnd = CreateWindowW(cls.lpszClassName, L"Fullscreen Test", WS_OVERLAPPEDWINDOW, 100, 100, + 800, 600, nullptr, nullptr, cls.hInstance, nullptr); + Check(hwnd != nullptr, "created top-level host window"); + if (!hwnd) + return 1; + + // Keep the child below y=30 so the parent's top caption band (40, 15) is + // directly inspectable for classic WM_NCPAINT overdraw. + g_child = CreateWindowW(L"STATIC", L"", WS_CHILD, 0, 0, 100, 100, hwnd, nullptr, cls.hInstance, + nullptr); + Check(g_child != nullptr, "created child surface window"); + + ShowWindow(hwnd, SW_SHOWNOACTIVATE); + Pump(); + Sleep(50); + Pump(); + + nativeapi::Window window(hwnd); + window.SetTitleBarStyle(nativeapi::TitleBarStyle::Hidden); + SetWindowSubclass(hwnd, OuterWatchProc, 1, 0); + Pump(); + Sleep(50); + Pump(); + + RECT initial_client = {}; + GetClientRect(hwnd, &initial_client); + const int initial_w = initial_client.right - initial_client.left; + const int initial_h = initial_client.bottom - initial_client.top; + Check(HiddenTitleBarTopAligned(hwnd), "hidden title bar aligns client top with window top"); + + MONITORINFO monitor = {sizeof(monitor)}; + GetMonitorInfoW(MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST), &monitor); + const int mon_w = monitor.rcMonitor.right - monitor.rcMonitor.left; + const int mon_h = monitor.rcMonitor.bottom - monitor.rcMonitor.top; + + // 1. Normal -> FullScreen -> Normal (watching top band for classic caption overdraw) + const int before_enter_sizes = g_size_count; + window.SetFullScreen(true); + Pump(); + Sleep(80); + Pump(); + RECT fs_client = {}; + GetClientRect(hwnd, &fs_client); + Check(window.IsFullScreen(), "entered fullscreen from normal"); + Check(g_size_count > before_enter_sizes && g_last_size_w == mon_w && g_last_size_h == mon_h, + "enter fullscreen delivers WM_SIZE matching monitor client size"); + Check(ChildMatchesClient(hwnd, g_child) && (fs_client.right - fs_client.left) == mon_w && + (fs_client.bottom - fs_client.top) == mon_h, + "child surface resizes to fullscreen client area"); + + g_watching_top_band = true; + g_top_band_polluted = false; + const int before_exit_sizes = g_size_count; + window.SetFullScreen(false); + Pump(); + Sleep(150); + Pump(); + g_watching_top_band = false; + + RECT restored_client = {}; + GetClientRect(hwnd, &restored_client); + Check(!window.IsFullScreen(), "exited fullscreen to normal"); + Check(g_size_count > before_exit_sizes && g_last_size_w == initial_w && + g_last_size_h == initial_h, + "exit fullscreen delivers WM_SIZE matching restored client size"); + Check(ChildMatchesClient(hwnd, g_child) && + (restored_client.right - restored_client.left) == initial_w && + (restored_client.bottom - restored_client.top) == initial_h, + "child surface shrinks back to restored client area without overflowing"); + Check(HiddenTitleBarTopAligned(hwnd), + "hidden title bar remains top-aligned after exiting fullscreen"); + Check(!g_top_band_polluted, + "WM_NCPAINT never paints classic title bar over top client band during fullscreen exit"); + { + BOOL dwm_nc = FALSE; + DwmGetWindowAttribute(hwnd, DWMWA_NCRENDERING_ENABLED, &dwm_nc, sizeof(dwm_nc)); + RECT rgn_box = {}, wr = {}; + GetWindowRect(hwnd, &wr); + const int w = wr.right - wr.left; + const int h = wr.bottom - wr.top; + HDC wdc = GetWindowDC(hwnd); + HDC mem = CreateCompatibleDC(wdc); + HBITMAP bmp = CreateCompatibleBitmap(wdc, w, h); + HGDIOBJ old = SelectObject(mem, bmp); + PrintWindow(hwnd, mem, 2); + const COLORREF pw_left = GetPixel(mem, 2, h / 2); + const COLORREF pw_right = GetPixel(mem, w - 3, h / 2); + const COLORREF pw_bottom = GetPixel(mem, w / 2, h - 3); + SelectObject(mem, old); + DeleteObject(bmp); + DeleteDC(mem); + ReleaseDC(hwnd, wdc); + Check(dwm_nc != FALSE && GetWindowRgnBox(hwnd, &rgn_box) == ERROR && pw_left == 0 && + pw_right == 0 && pw_bottom == 0, + "left, right, and bottom DWM resize borders stay transparent without classic frame"); + } + + // 2. Classic UxTheme caption-painting messages must be suppressed for hidden title bar. + constexpr UINT kNcUahDrawCaption = 0x00AE; + constexpr UINT kNcUahDrawFrame = 0x00AF; + Check(SendMessageW(hwnd, kNcUahDrawCaption, 0, 0) == 0 && + SendMessageW(hwnd, kNcUahDrawFrame, 0, 0) == 0, + "hidden title bar suppresses WM_NCUAHDRAWCAPTION and WM_NCUAHDRAWFRAME"); + + // 3. Maximized -> FullScreen -> Maximized + ShowWindow(hwnd, SW_SHOWMAXIMIZED); + Pump(); + RECT max_client = {}; + GetClientRect(hwnd, &max_client); + const int max_w = max_client.right - max_client.left; + const int max_h = max_client.bottom - max_client.top; + + window.SetFullScreen(true); + Pump(); + Check(window.IsFullScreen() && ChildMatchesClient(hwnd, g_child) && g_last_size_w == mon_w && + g_last_size_h == mon_h, + "maximized -> fullscreen resizes child surface to full monitor"); + + window.SetFullScreen(false); + Pump(); + RECT post_max_client = {}; + GetClientRect(hwnd, &post_max_client); + Check(!window.IsFullScreen() && IsZoomed(hwnd), "exited fullscreen back to maximized"); + Check(ChildMatchesClient(hwnd, g_child) && + (post_max_client.right - post_max_client.left) == max_w && + (post_max_client.bottom - post_max_client.top) == max_h, + "maximized -> fullscreen -> maximized restores child surface to work area"); + + DestroyWindow(hwnd); + DeleteObject(g_green_brush); + return failures ? 1 : 0; +}