admin管理员组

文章数量:1317898

I am looking at this GitHub project:

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:

The controls I have issues with are:

  • Menu bar
  • Tool bar
  • Check box
  • Group box
  • Combo - Droplist
  • Date picker
  • Calendar
  • Property Grid

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;
}

I am looking at this GitHub project:

https://github/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:

The controls I have issues with are:

  • Menu bar
  • Tool bar
  • Check box
  • Group box
  • Combo - Droplist
  • Date picker
  • Calendar
  • Property Grid

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;
}
Share Improve this question edited Jan 22 at 17:51 Andrew Truckle asked Jan 22 at 15:59 Andrew TruckleAndrew Truckle 19.2k17 gold badges84 silver badges218 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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/SFTRS/DarkTaskDialog

本文标签: visual cUsing dark mode for Win32 statusbarStack Overflow