Add WinStructWordSize<> helper and use it

This is similar to WinStruct<> but initializes dwSize and not cbSize
struct member.

In the future we should auto-detect the xxSize existence in WinStruct
using either SFINAE or, preferably, concepts, but for now keep things
simple.
This commit is contained in:
Vadim Zeitlin
2026-06-08 02:44:16 +02:00
parent 847561bdf6
commit 3de0aec9a5
8 changed files with 31 additions and 23 deletions
+17
View File
@@ -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 <class T>
struct WinStructWordSize : public T
{
WinStructWordSize()
{
wxZeroMemory(*this);
this->dwSize = sizeof(T);
}
};
// Macros for converting wxString to the type expected by API functions.
//
+1 -2
View File
@@ -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<DTTOPTS> textOpts;
textOpts.dwFlags = DTT_TEXTCOLOR;
textOpts.crText = wxColourToRGB(GetMenuColour(colText));
+1 -2
View File
@@ -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<INITCOMMONCONTROLSEX> icex;
icex.dwICC = ICC_DATE_CLASSES;
// see comment in wxApp::GetComCtl32Version() explaining the
+5 -10
View File
@@ -266,9 +266,8 @@ bool wxJoystick::GetButtonState(unsigned id) const
int wxJoystick::GetPOVPosition() const
{
#ifndef NO_JOYGETPOSEX
JOYINFOEX joyInfo;
WinStructWordSize<JOYINFOEX> 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<JOYINFOEX> 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<JOYINFOEX> 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<JOYINFOEX> 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<JOYINFOEX> joyInfo;
joyInfo.dwFlags = JOY_RETURNV;
joyInfo.dwSize = sizeof(joyInfo);
MMRESULT res = joyGetPosEx(m_joystick, & joyInfo);
if (res == JOYERR_NOERROR )
{
+1 -2
View File
@@ -956,8 +956,7 @@ void wxRendererXP::DrawItemText(wxWindow* win,
{
RECT rc = ConvertToRECT(dc, rect);
DTTOPTS textOpts;
textOpts.dwSize = sizeof(textOpts);
WinStructWordSize<DTTOPTS> textOpts;
textOpts.dwFlags = DTT_STATEID;
textOpts.iStateId = itemState;
+4 -2
View File
@@ -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<DTTOPTS> 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<int>(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<DTTOPTS> opts;
opts.dwFlags = DTT_COMPOSITED | DTT_TEXTCOLOR;
opts.crText = TDDarkCol::kTextNormal;
LOGFONT lf = {};
+1 -2
View File
@@ -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<COLORSCHEME> colScheme;
colScheme.clrBtnHighlight =
colScheme.clrBtnShadow = wxSysColourToRGB(wxSYS_COLOUR_WINDOW);
::SendMessage(GetHwnd(), TB_SETCOLORSCHEME, 0, (LPARAM)&colScheme);
+1 -3
View File
@@ -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<PROCESSENTRY32> pe;
// Walk the snapshot of the processes, and for each process,
// kill it if its parent is pid.