Fix illegal calls to DwmGetWindowAttribute()

DWMWA_WINDOW_CORNER_PREFERENCE and DWMWA_BORDER_COLOR are only supported for DwmSetWindowAttribute(), they can't be queried.

Fixes https://github.com/libsdl-org/SDL/issues/12206
Closes https://github.com/libsdl-org/SDL/pull/12209
This commit is contained in:
Sam Lantinga 2025-03-19 22:00:42 -07:00
parent 96bf12444c
commit f2ed5c7a1b
2 changed files with 8 additions and 23 deletions

View File

@ -447,7 +447,6 @@ static bool SetupWindowData(SDL_VideoDevice *_this, SDL_Window *window, HWND hwn
data->videodata = videodata; data->videodata = videodata;
data->initializing = true; data->initializing = true;
data->last_displayID = window->last_displayID; data->last_displayID = window->last_displayID;
data->dwma_border_color = DWMWA_COLOR_DEFAULT;
data->hint_erase_background_mode = GetEraseBackgroundModeHint(); data->hint_erase_background_mode = GetEraseBackgroundModeHint();
@ -1265,42 +1264,30 @@ void WIN_RestoreWindow(SDL_VideoDevice *_this, SDL_Window *window)
} }
} }
static DWM_WINDOW_CORNER_PREFERENCE WIN_UpdateCornerRoundingForHWND(HWND hwnd, DWM_WINDOW_CORNER_PREFERENCE cornerPref) static void WIN_UpdateCornerRoundingForHWND(HWND hwnd, DWM_WINDOW_CORNER_PREFERENCE cornerPref)
{ {
DWM_WINDOW_CORNER_PREFERENCE oldPref = DWMWCP_DEFAULT;
SDL_SharedObject *handle = SDL_LoadObject("dwmapi.dll"); SDL_SharedObject *handle = SDL_LoadObject("dwmapi.dll");
if (handle) { if (handle) {
DwmGetWindowAttribute_t DwmGetWindowAttributeFunc = (DwmGetWindowAttribute_t)SDL_LoadFunction(handle, "DwmGetWindowAttribute");
DwmSetWindowAttribute_t DwmSetWindowAttributeFunc = (DwmSetWindowAttribute_t)SDL_LoadFunction(handle, "DwmSetWindowAttribute"); DwmSetWindowAttribute_t DwmSetWindowAttributeFunc = (DwmSetWindowAttribute_t)SDL_LoadFunction(handle, "DwmSetWindowAttribute");
if (DwmGetWindowAttributeFunc && DwmSetWindowAttributeFunc) { if (DwmSetWindowAttributeFunc) {
DwmGetWindowAttributeFunc(hwnd, DWMWA_WINDOW_CORNER_PREFERENCE, &oldPref, sizeof(oldPref));
DwmSetWindowAttributeFunc(hwnd, DWMWA_WINDOW_CORNER_PREFERENCE, &cornerPref, sizeof(cornerPref)); DwmSetWindowAttributeFunc(hwnd, DWMWA_WINDOW_CORNER_PREFERENCE, &cornerPref, sizeof(cornerPref));
} }
SDL_UnloadObject(handle); SDL_UnloadObject(handle);
} }
return oldPref;
} }
static COLORREF WIN_UpdateBorderColorForHWND(HWND hwnd, COLORREF colorRef) static void WIN_UpdateBorderColorForHWND(HWND hwnd, COLORREF colorRef)
{ {
COLORREF oldPref = DWMWA_COLOR_DEFAULT;
SDL_SharedObject *handle = SDL_LoadObject("dwmapi.dll"); SDL_SharedObject *handle = SDL_LoadObject("dwmapi.dll");
if (handle) { if (handle) {
DwmGetWindowAttribute_t DwmGetWindowAttributeFunc = (DwmGetWindowAttribute_t)SDL_LoadFunction(handle, "DwmGetWindowAttribute");
DwmSetWindowAttribute_t DwmSetWindowAttributeFunc = (DwmSetWindowAttribute_t)SDL_LoadFunction(handle, "DwmSetWindowAttribute"); DwmSetWindowAttribute_t DwmSetWindowAttributeFunc = (DwmSetWindowAttribute_t)SDL_LoadFunction(handle, "DwmSetWindowAttribute");
if (DwmGetWindowAttributeFunc && DwmSetWindowAttributeFunc) { if (DwmSetWindowAttributeFunc) {
DwmGetWindowAttributeFunc(hwnd, DWMWA_BORDER_COLOR, &oldPref, sizeof(oldPref));
DwmSetWindowAttributeFunc(hwnd, DWMWA_BORDER_COLOR, &colorRef, sizeof(colorRef)); DwmSetWindowAttributeFunc(hwnd, DWMWA_BORDER_COLOR, &colorRef, sizeof(colorRef));
} }
SDL_UnloadObject(handle); SDL_UnloadObject(handle);
} }
return oldPref;
} }
/** /**
@ -1366,13 +1353,13 @@ SDL_FullscreenResult WIN_SetWindowFullscreen(SDL_VideoDevice *_this, SDL_Window
} }
// Disable corner rounding & border color (Windows 11+) so the window fills the full screen // Disable corner rounding & border color (Windows 11+) so the window fills the full screen
data->windowed_mode_corner_rounding = WIN_UpdateCornerRoundingForHWND(hwnd, DWMWCP_DONOTROUND); WIN_UpdateCornerRoundingForHWND(hwnd, DWMWCP_DONOTROUND);
data->dwma_border_color = WIN_UpdateBorderColorForHWND(hwnd, DWMWA_COLOR_NONE); WIN_UpdateBorderColorForHWND(hwnd, DWMWA_COLOR_NONE);
} else { } else {
BOOL menu; BOOL menu;
WIN_UpdateCornerRoundingForHWND(hwnd, (DWM_WINDOW_CORNER_PREFERENCE)data->windowed_mode_corner_rounding); WIN_UpdateCornerRoundingForHWND(hwnd, DWMWCP_DEFAULT);
WIN_UpdateBorderColorForHWND(hwnd, data->dwma_border_color); WIN_UpdateBorderColorForHWND(hwnd, DWMWA_COLOR_DEFAULT);
/* Restore window-maximization state, as applicable. /* Restore window-maximization state, as applicable.
Special care is taken to *not* do this if and when we're Special care is taken to *not* do this if and when we're

View File

@ -87,8 +87,6 @@ struct SDL_WindowData
RECT initial_size_rect; RECT initial_size_rect;
RECT cursor_clipped_rect; // last successfully committed clipping rect for this window RECT cursor_clipped_rect; // last successfully committed clipping rect for this window
RECT cursor_ctrlock_rect; // this is Windows-specific, but probably does not need to be per-window RECT cursor_ctrlock_rect; // this is Windows-specific, but probably does not need to be per-window
UINT windowed_mode_corner_rounding;
COLORREF dwma_border_color;
bool mouse_tracked; bool mouse_tracked;
bool destroy_parent_with_window; bool destroy_parent_with_window;
SDL_DisplayID last_displayID; SDL_DisplayID last_displayID;