From 1848ce680b7f6002deda0770f9238cbbde7cbb6f Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Fri, 17 Jan 2025 15:23:41 -0500
Subject: [PATCH] win32: Use the window coordinates to get the monitor when
de-minimizing a maximized window
MonitorFromWindow can fail if called on a window being de-minimized, so fall back to using the monitor from the last window coordinates if initial retrieval fails.
---
src/video/windows/SDL_windowsevents.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/src/video/windows/SDL_windowsevents.c b/src/video/windows/SDL_windowsevents.c
index 1a91e03d7d7e0..459f43dfa5202 100644
--- a/src/video/windows/SDL_windowsevents.c
+++ b/src/video/windows/SDL_windowsevents.c
@@ -2022,7 +2022,12 @@ LRESULT CALLBACK WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara
WINDOWPLACEMENT placement;
if (GetWindowPlacement(hwnd, &placement) && placement.showCmd == SW_MAXIMIZE) {
// Maximized borderless windows should use the monitor work area.
- HMONITOR hMonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
+ HMONITOR hMonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONULL);
+ if (!hMonitor) {
+ // The returned monitor can be null when restoring from minimized, so use the last coordinates.
+ const POINT pt = { data->window->windowed.x, data->window->windowed.y };
+ hMonitor = MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
+ }
if (hMonitor) {
MONITORINFO info;
SDL_zero(info);