Skip to content

Commit

Permalink
Defuse wShowWindow behavior in Windows influencing the initial window…
Browse files Browse the repository at this point in the history
… created

#9434
  • Loading branch information
learn-more committed Oct 23, 2024
1 parent 60a2872 commit 4ee163f
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/video/windows/SDL_windowsmessagebox.c
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,39 @@ static bool WIN_ShowOldMessageBox(const SDL_MessageBoxData *messageboxdata, int
return result;
}


static INT_PTR CALLBACK EmptyDlgProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
return 0;
}

// Windows stores an initial window state passed in by the shell (or another process) if STARTF_USESHOWWINDOW is set.
// This state is applied to the first window that receives a 'ShowWindow' call.
// To ensure our window is not influenced by this, we create a dummy window to catch this initial state.
// The idea is taken from putty, which uses this method to ensure its windows are always created in the correct state.
static bool s_DefusedInitialwShowWindow = false;
static void DefuseInitialwShowWindow(void)
{
// Build up a dialog template
enum { DlgX = 0, DlgY = 0, DlgW = 100, DlgH = 60, ChildCount = 0, MenuId = 0, ClassName = 0, Title = 0, DialogStyleExLo = 0, DialogStyleExHi = 0 };
const UINT DialogStyle = DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU;
static const WORD DialogTemplate[] =
{
LOWORD(DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU), HIWORD(DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU), DialogStyleExLo, DialogStyleExHi, ChildCount, DlgX, DlgY, DlgW, DlgH, MenuId, ClassName, Title,
};
if (s_DefusedInitialwShowWindow) {
return;
}
// Create a window using the template
HWND hWnd = CreateDialogIndirectParamW(NULL, (LPCDLGTEMPLATE)DialogTemplate, NULL, EmptyDlgProc, 0);
// Now catch the window state provided
ShowWindow(hWnd, SW_HIDE);
// We are done
DestroyWindow(hWnd);
s_DefusedInitialwShowWindow = true;
}


/* TaskDialogIndirect procedure
* This is because SDL targets Windows XP (0x501), so this is not defined in the platform SDK.
*/
Expand Down Expand Up @@ -930,6 +963,8 @@ bool WIN_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonID)
return SDL_OutOfMemory();
}

DefuseInitialwShowWindow();

// If we cannot load comctl32.dll use the old messagebox!
hComctl32 = LoadLibrary(TEXT("comctl32.dll"));
if (!hComctl32) {
Expand Down

0 comments on commit 4ee163f

Please sign in to comment.