diff --git a/include/wx/msw/private.h b/include/wx/msw/private.h index 0163b81b6a..06eb8bb382 100644 --- a/include/wx/msw/private.h +++ b/include/wx/msw/private.h @@ -195,6 +195,23 @@ struct WinStruct : public T } }; +// Life wouldn't be fun if Windows didn't call the size member differently in +// different structs, so define the equivalent of the above for the ones where +// it's called dwSize. +// +// When we can require C++17 or preferably C++20 we could merge this with +// WinStruct by detecting the presence of cbSize/dwSize member using SFINAE, +// but for now just define it separately to keep things simple. +template +struct WinStructWordSize : public T +{ + WinStructWordSize() + { + wxZeroMemory(*this); + + this->dwSize = sizeof(T); + } +}; // Macros for converting wxString to the type expected by API functions. // diff --git a/src/msw/darkmode.cpp b/src/msw/darkmode.cpp index 8b4231dd86..41945afad1 100644 --- a/src/msw/darkmode.cpp +++ b/src/msw/darkmode.cpp @@ -774,8 +774,7 @@ HandleMenuMessage(WXLRESULT* result, // We have to specify the text colour explicitly as by default // black would be used, making the menu label unreadable on the // (almost) black background. - DTTOPTS textOpts; - textOpts.dwSize = sizeof(textOpts); + WinStructWordSize textOpts; textOpts.dwFlags = DTT_TEXTCOLOR; textOpts.crText = wxColourToRGB(GetMenuColour(colText)); diff --git a/src/msw/datecontrols.cpp b/src/msw/datecontrols.cpp index f0b3444c19..4ffd2ef2d4 100644 --- a/src/msw/datecontrols.cpp +++ b/src/msw/datecontrols.cpp @@ -51,8 +51,7 @@ bool wxMSWDateControls::CheckInitialization() // it's enough to give the error only once s_initResult = false; - INITCOMMONCONTROLSEX icex; - icex.dwSize = sizeof(icex); + WinStructWordSize icex; icex.dwICC = ICC_DATE_CLASSES; // see comment in wxApp::GetComCtl32Version() explaining the diff --git a/src/msw/joystick.cpp b/src/msw/joystick.cpp index c39e2de89e..77297a06c4 100644 --- a/src/msw/joystick.cpp +++ b/src/msw/joystick.cpp @@ -266,9 +266,8 @@ bool wxJoystick::GetButtonState(unsigned id) const int wxJoystick::GetPOVPosition() const { #ifndef NO_JOYGETPOSEX - JOYINFOEX joyInfo; + WinStructWordSize joyInfo; joyInfo.dwFlags = JOY_RETURNPOV; - joyInfo.dwSize = sizeof(joyInfo); MMRESULT res = joyGetPosEx(m_joystick, & joyInfo); if (res == JOYERR_NOERROR ) { @@ -288,9 +287,8 @@ int wxJoystick::GetPOVPosition() const int wxJoystick::GetPOVCTSPosition() const { #ifndef NO_JOYGETPOSEX - JOYINFOEX joyInfo; + WinStructWordSize joyInfo; joyInfo.dwFlags = JOY_RETURNPOVCTS; - joyInfo.dwSize = sizeof(joyInfo); MMRESULT res = joyGetPosEx(m_joystick, & joyInfo); if (res == JOYERR_NOERROR ) { @@ -306,9 +304,8 @@ int wxJoystick::GetPOVCTSPosition() const int wxJoystick::GetRudderPosition() const { #ifndef NO_JOYGETPOSEX - JOYINFOEX joyInfo; + WinStructWordSize joyInfo; joyInfo.dwFlags = JOY_RETURNR; - joyInfo.dwSize = sizeof(joyInfo); MMRESULT res = joyGetPosEx(m_joystick, & joyInfo); if (res == JOYERR_NOERROR ) { @@ -324,9 +321,8 @@ int wxJoystick::GetRudderPosition() const int wxJoystick::GetUPosition() const { #ifndef NO_JOYGETPOSEX - JOYINFOEX joyInfo; + WinStructWordSize joyInfo; joyInfo.dwFlags = JOY_RETURNU; - joyInfo.dwSize = sizeof(joyInfo); MMRESULT res = joyGetPosEx(m_joystick, & joyInfo); if (res == JOYERR_NOERROR ) { @@ -342,9 +338,8 @@ int wxJoystick::GetUPosition() const int wxJoystick::GetVPosition() const { #ifndef NO_JOYGETPOSEX - JOYINFOEX joyInfo; + WinStructWordSize joyInfo; joyInfo.dwFlags = JOY_RETURNV; - joyInfo.dwSize = sizeof(joyInfo); MMRESULT res = joyGetPosEx(m_joystick, & joyInfo); if (res == JOYERR_NOERROR ) { diff --git a/src/msw/renderer.cpp b/src/msw/renderer.cpp index 1366fb8704..f0f0056464 100644 --- a/src/msw/renderer.cpp +++ b/src/msw/renderer.cpp @@ -956,8 +956,7 @@ void wxRendererXP::DrawItemText(wxWindow* win, { RECT rc = ConvertToRECT(dc, rect); - DTTOPTS textOpts; - textOpts.dwSize = sizeof(textOpts); + WinStructWordSize textOpts; textOpts.dwFlags = DTT_STATEID; textOpts.iStateId = itemState; diff --git a/src/msw/taskdlg.cpp b/src/msw/taskdlg.cpp index 6da0d2e4f3..f2434973bf 100644 --- a/src/msw/taskdlg.cpp +++ b/src/msw/taskdlg.cpp @@ -605,7 +605,8 @@ static void TDPaintText(HDC hdc, const TDPageState& s) if (!native) { - DTTOPTS opts = { sizeof(opts) }; opts.dwFlags = DTT_COMPOSITED | DTT_TEXTCOLOR; + WinStructWordSize opts; + opts.dwFlags = DTT_COMPOSITED | DTT_TEXTCOLOR; opts.crText = TDGetTextColour(s, part); FillRect(hdc, &rcT, brBg); ::DrawThemeTextEx(s.hTD, hdc, part, 0, el.name.c_str(), -1, dtF, &rcT, &opts); @@ -780,7 +781,8 @@ static LRESULT CALLBACK TDRadioButtonSubclassProc( GetWindowTextW(hwnd, text, static_cast(std::size(text))); auto gs = hBtn.GetTrueSize(BP_RADIOBUTTON, RBS_UNCHECKEDNORMAL); RECT rcT = { gs.x + 2,0,rcC.right,rcC.bottom }; - DTTOPTS opts = { sizeof(opts) }; + + WinStructWordSize opts; opts.dwFlags = DTT_COMPOSITED | DTT_TEXTCOLOR; opts.crText = TDDarkCol::kTextNormal; LOGFONT lf = {}; diff --git a/src/msw/toolbar.cpp b/src/msw/toolbar.cpp index 26bfe376ac..108a732bed 100644 --- a/src/msw/toolbar.cpp +++ b/src/msw/toolbar.cpp @@ -734,8 +734,7 @@ void wxToolBar::MSWSetDarkOrLightMode(SetMode setmode) // Update the separator above the toolbar which is drawn partially in // white by default and so looks very ugly in dark mode. - COLORSCHEME colScheme; - colScheme.dwSize = sizeof(COLORSCHEME); + WinStructWordSize colScheme; colScheme.clrBtnHighlight = colScheme.clrBtnShadow = wxSysColourToRGB(wxSYS_COLOUR_WINDOW); ::SendMessage(GetHwnd(), TB_SETCOLORSCHEME, 0, (LPARAM)&colScheme); diff --git a/src/msw/utils.cpp b/src/msw/utils.cpp index 84adee7784..d905964ed9 100644 --- a/src/msw/utils.cpp +++ b/src/msw/utils.cpp @@ -818,9 +818,7 @@ int wxKillAllChildren(long pid, wxSignal sig, wxKillError *krc, int flags) } //Fill in the size of the structure before using it. - PROCESSENTRY32 pe; - wxZeroMemory(pe); - pe.dwSize = sizeof(PROCESSENTRY32); + WinStructWordSize pe; // Walk the snapshot of the processes, and for each process, // kill it if its parent is pid.