Merge branch 'msw-button-fg-col'
Unix builds / Ubuntu 18.04 wxGTK 3 compatible 3.0 (push) Has been cancelled
Unix builds / Ubuntu 24.04 wxGTK ASAN not compatible (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxGTK UTF-8 (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxQt (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxX11 (push) Has been cancelled
Unix builds / Ubuntu 20.04 wxGTK 3 with clang (push) Has been cancelled
Unix builds / Ubuntu 22.04 wxGTK with wx containers (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxDFB (push) Has been cancelled
Unix builds / Ubuntu 24.04 wxGTK UBSAN (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxGTK 3 static with gcc 4.8 (push) Has been cancelled
Unix builds / Ubuntu 18.04 wxGTK 2 (push) Has been cancelled
CMake builds / Ubuntu 22.04 wxGTK 3 (push) Has been cancelled
CMake builds / macOS latest wxGTK 3 Unix Makefiles (push) Has been cancelled
CMake builds / MSW/MSVC wxMSW (push) Has been cancelled
CMake builds / MSW/Clang wxMSW (push) Has been cancelled
CMake builds / macOS latest wxOSX Ninja (push) Has been cancelled
CMake builds / macOS 14 wxOSX Xcode (push) Has been cancelled
CMake builds / macOS 14 wxIOS (push) Has been cancelled
CMake builds / MSW/MSVC wxQt 5.15 (push) Has been cancelled
CMake builds / MSW/MSVC wxQt 6.10 (push) Has been cancelled
Mac builds / wxMac ARM ASAN not compatible (push) Has been cancelled
Mac builds / wxMac Universal C++14 (push) Has been cancelled
Mac builds / wxiOS Simulator on Silicon Mac (push) Has been cancelled
Mac builds / wxiOS (push) Has been cancelled
Mac builds / wxMac Intel C++17 (push) Has been cancelled
Mac Xcode builds / iOS Simulator static (push) Has been cancelled
Mac Xcode builds / macOS dynamic Release (push) Has been cancelled
Mac Xcode builds / iOS static Debug (push) Has been cancelled
MSW builds / wxMSW vs2022 DLL Debug x64 (push) Has been cancelled
MSW builds / wxMSW vs2022 DLL Release x64 (push) Has been cancelled
MSW builds / wxMSW vs2022 Debug Win32 (push) Has been cancelled
MSW builds / wxMSW vs2022 Release arm64 (push) Has been cancelled
MSW builds / wxMSW vs2026 DLL Release x64 (push) Has been cancelled
MSW cross-builds / wxMSW 64 bits not compatible (push) Has been cancelled
MSW cross-builds / wxMSW/Univ (push) Has been cancelled
MSW cross-builds / wxMSW 32 bits (push) Has been cancelled
Code Checks / Check Spelling (push) Has been cancelled
Code Checks / Check Whitespace (push) Has been cancelled
Code Checks / Check Mixed EOL (push) Has been cancelled
Code Checks / Check C++ Style (push) Has been cancelled
Code Checks / Check All Headers In allheaders.h (push) Has been cancelled
Update Documentation / Update Online Documentation (push) Has been cancelled

Respect user-set button foreground colour in wxMSW.

See #26618.
This commit is contained in:
Vadim Zeitlin
2026-06-22 14:34:26 +02:00
+55 -19
View File
@@ -1262,10 +1262,59 @@ void DrawXPBackground(wxAnyButton *button, HDC hdc, RECT& rectBtn, UINT state)
TMT_CONTENTMARGINS, &rectBtn, &margins);
::InflateRect(&rectBtn, -margins.cxLeftWidth, -margins.cyTopHeight);
if ( button->UseBgCol() && iState != PBS_HOT )
if ( button->UseBgCol() )
{
COLORREF colBg = wxColourToRGB(button->GetBackgroundColour());
AutoHBRUSH hbrushBackground(colBg);
wxColour col = button->GetBackgroundColour();
// We don't currently have a way to specify a different background
// colour for the hot/current state, but we want it to be visually
// different from the normal state, so construct a slightly different
// shade of this colour automatically.
#if wxUSE_IMAGE
if ( iState == PBS_HOT )
{
wxImage::RGBValue rgb(col.Red(), col.Green(), col.Blue());
wxImage::HSVValue hsv = wxImage::RGBtoHSV(rgb);
// If the background is light, make it slightly darker, otherwise
// make it lighter.
//
// Note that we assume that the contrast with the text will remain
// good enough, a better solution would be to check the contrast
// and somehow find the shade of the background colour which still
// gives sufficient contrast with the text colour, but this is more
// complicated and this simple version works well enough for the
// colours with sufficient contrast.
double valueHot = hsv.value;
if ( hsv.value < 0.5 )
{
valueHot += 0.2;
// Don't make it completely white.
if ( valueHot > 0.95 )
{
// But also don't make it darker than the original colour.
valueHot = wxMax(0.95, hsv.value);
}
}
else // Same logic as above, but in reverse.
{
valueHot -= 0.2;
if ( valueHot < 0.05 )
{
hsv.value = wxMin(0.05, hsv.value);
}
}
hsv.value = valueHot;
rgb = wxImage::HSVtoRGB(hsv);
col = wxColour(rgb.red, rgb.green, rgb.blue);
}
#endif // wxUSE_IMAGE
AutoHBRUSH hbrushBackground(wxColourToRGB(col));
FillRect(hdc, &rectBtn, hbrushBackground);
}
@@ -1506,22 +1555,9 @@ bool wxAnyButton::MSWOnDraw(WXDRAWITEMSTRUCT *wxdis)
// finally draw the label
if ( ShowsLabel() )
{
COLORREF colFg;
if ( state & ODS_DISABLED )
{
colFg = ::GetSysColor(COLOR_GRAYTEXT);
}
else if ( wxUxThemeIsActive() &&
GetButtonState(this, state) == wxAnyButton::State_Current )
{
// The button is highlighted, use the standard colour to ensure
// that its text is readable.
colFg = ::GetSysColor(COLOR_BTNTEXT);
}
else
{
colFg = wxColourToRGB(GetForegroundColour());
}
COLORREF colFg = state & ODS_DISABLED
? ::GetSysColor(COLOR_GRAYTEXT)
: wxColourToRGB(GetForegroundColour());
wxTextColoursChanger changeFg(hdc, colFg, CLR_INVALID);
wxBkModeChanger changeBkMode(hdc, wxBRUSHSTYLE_TRANSPARENT);