CComboBoxEx in dark mode (with Dropdown style) in MFC

ChuckieAJ 201 Reputation points
2025-02-14T18:32:24.68+00:00

I am trying to implement a dark mode for my application, by using DarkMode_Explorer / DarkMode_CFD etc. I have handled OnCtlColor and set the theme using SetWindowTheme.

If my CComboBoxEx is a DropList then it renderings properly. But when I set it to Downdown it looks odd:

User's image

User's image

Using a standard CComboBox renders properly for both modes.

I wanted to use the CComboBoxEx because it made it easier to get at the edit control and set it as numeric. I realise I can do it old-style with CComboBox. But it would be nice to continue using this control.

Steps to reproduce

  1. Add a CComBoxEx control to your dialog and set it as Dropdown.
  2. Configure your application / dialog to use dark mode.
  3. Apply the theme to the combo:
pComboBoxEx->SetWindowTheme(theme);
auto pComboEdit = pComboBoxEx->GetEditCtrl();
if (pComboEdit && pComboEdit->GetSafeHwnd())
{
	SetWindowTheme(pComboEdit->GetSafeHwnd(), L"DarkMode_CFD", nullptr);
}

// Get the underlying ComboBox control so that we can get the ListBox handle
auto pComboBox = pComboBoxEx->GetComboBoxCtrl();
if (pComboBox)
{
	COMBOBOXINFO cbi{};
	cbi.cbSize = sizeof(COMBOBOXINFO);
	if (GetComboBoxInfo(pComboBox->GetSafeHwnd(), &cbi) && cbi.hwndList)
	{
		SetWindowTheme(cbi.hwndList, L"DarkMode_Explorer", nullptr);
		// The scrollbars are now set correctly, but the ListBox background is still white.
	}
}
  1. Handle WM_CTLCOLOR (I am including all I am using in my project):
const auto backColour = parentIsChild ? DarkModeTools::kDarkSheetBackgroundColor
	: DarkModeTools::kDarkBackgroundColor;
switch (nCtlColor)
{
case CTLCOLOR_STATIC: // Static text
	pDC->SetBkColor(backColour);
	if (pControl->IsWindowEnabled())
	{
		pDC->SetTextColor(kDarkTextColor);
	}
	else
	{
		pDC->SetTextColor(::GetSysColor(COLOR_GRAYTEXT));
	}
	break;
case CTLCOLOR_DLG: // Dialog background
case CTLCOLOR_SCROLLBAR: // Scrollbars
case CTLCOLOR_EDIT: // Edit control
case CTLCOLOR_LISTBOX: // Listbox
case CTLCOLOR_MSGBOX:
	pDC->SetTextColor(kDarkTextColor);
	pDC->SetBkColor(backColour);
	break;
case CTLCOLOR_BTN: // Buttons
default:
	// You can add specific cases here if needed
	break;
}

Hat should be enough to replicate the issue.

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,737 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,856 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Minxin Yu 12,681 Reputation points Microsoft Vendor
    2025-02-17T07:49:07.33+00:00

    Hi,

    I can't change the color of scrollbar using the sample code.

    I edited the drawing of CTLCOLOR_LISTBOX

    	case CTLCOLOR_LISTBOX:
    	{
    		pDC->SetTextColor (RGB (250, 250, 250));
    		static CBrush brush (RGB (0, 0, 0));
    		return brush;
    	}
    

    User's image

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.