Andrew Truckle
Andrew Truckle

Reputation: 19197

Using dark mode for Win32 statusbar

I am looking at this GitHub project:

https://github.com/ysc3839/win32-darkmode

It refers to using undocumented API to use dark mode in Win32 projects (if the OS is of a certain version).

I find that it works, but not for status bars (and a few other controls). I tried:

SetWindowTheme(m_StatusBar.GetSafeHwnd(), L"DarkMode_Explorer", nullptr);

But it made no difference:

enter image description here

The controls I have issues with are:

I include my OnCtlColor handler for completeness (in-case it helps):

HBRUSH CResizingDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = __super::OnCtlColor(pDC, pWnd, nCtlColor);

    if (theApp.GetNumberSetting(L"Options", L"Theme_UseDarkMode", false))
    {
        constexpr COLORREF darkBkColor = 0x383838;
        constexpr COLORREF darkTextColor = 0xFFFFFF;

        // Return a solid brush with the dark background color
        static HBRUSH hbrBackground = CreateSolidBrush(RGB(45, 45, 45));
        hbr = hbrBackground;

        // If you need to customize specific controls, add conditions
        switch (nCtlColor)
        {
        case CTLCOLOR_DLG: // Dialog background
        case CTLCOLOR_STATIC: // Static text
        case CTLCOLOR_EDIT: // Edit control
        case CTLCOLOR_LISTBOX: // Listbox
            pDC->SetTextColor(darkTextColor); // Light text
            pDC->SetBkColor(darkBkColor); // Dark background
            break;

        break;

        case CTLCOLOR_SCROLLBAR: // Scrollbars
        case CTLCOLOR_BTN: // Buttons
        default:
            // You can add specific cases here if needed
            break;
        }

    }

    return hbr;
}

Upvotes: 0

Views: 66

Answers (1)

Oleg Korzhukov
Oleg Korzhukov

Reputation: 681

As far as I understand, Microsoft has implemented dark theme support only for the controls they use themselves in different parts of Windows. Therefore, I do not expect the dark theme to ever work natively for controls like the calendar or time picker. Our solution is to use Microsoft Detours to intercept APIs like GetThemeColor and DrawThemeText, substitute color, and call the original function. It works fine for example with checkbox and radio button controls, but we never implemented workarounds for other controls you might need. You can check our open-source project: https://github.com/SFTRS/DarkTaskDialog

Upvotes: 1

Related Questions