Fix wxSpinButton dark mode switching under Windows 11 22H2

Add call to MSWSetDarkOrLightMode() upon control creation to get correct
dark mode drawing under Windows 11 22H2 and later and avoid inverting
colours if they are already drawn correctly for dark mode.

Closes #26683.
This commit is contained in:
Steve Cornett
2026-07-21 00:04:49 +02:00
committed by Vadim Zeitlin
parent aa52238256
commit 6de4ddb544
+11 -3
View File
@@ -144,6 +144,9 @@ bool wxSpinButton::Create(wxWindow *parent,
SetInitialSize(size);
if ( wxMSWDarkMode::IsActive() )
MSWSetDarkOrLightMode(SetMode::Initial);
return true;
}
@@ -178,8 +181,9 @@ void wxSpinButton::OnPaint(wxPaintEvent& event)
{
if ( wxMSWDarkMode::IsActive() )
{
// Unfortunately PaintIfNecessary() can't be used here as we need to
// handle the extra border below, so duplicate what it does here.
// Have the updown control draw itself, then modify the result with a
// border and possibly inverted colours.
const RECT rc = wxGetClientRect(GetHwnd());
const wxSize size{rc.right - rc.left, rc.bottom - rc.top};
@@ -213,6 +217,10 @@ void wxSpinButton::OnPaint(wxPaintEvent& event)
wxImage image = bmp.ConvertToImage();
// Prior to Windows 11 22H2 (build 22621), the updown control always
// draws in light mode, so invert the colours to make it dark.
const bool shouldInvert = !wxCheckOsVersion(10, 0, 22621);
const int width = image.GetWidth();
const int height = image.GetHeight();
unsigned char *data = image.GetData();
@@ -230,7 +238,7 @@ void wxSpinButton::OnPaint(wxPaintEvent& event)
if ( alpha )
*alpha = wxALPHA_OPAQUE;
}
else
else if ( shouldInvert )
{
// This uses a slightly different formula than the one in
// InvertBitmapPixel() because the one there results in the