Click here to Skip to main content
14,975,553 members
Articles / Desktop Programming / WTL
Posted 28 Jan 2004


64 bookmarked

Correctly drawn themed dialogs in WinXP

Rate me:
Please Sign up or sign in to vote.
4.91/5 (34 votes)
29 Jan 20047 min read
This article shows a workaround to the graphics bug that a checkbox or radio button is displayed with a black background on themed dialogs.


Ever needed or wanted to write an application which uses the 'new' and nifty Windows XP themes? Well, you might just have stumbled on a little problem when using tab controls and static controls in such an application. In case you create a dialog which has a tab control and on this tab control static controls or a radio button or a checkbox, the background color of those controls is not drawn correctly. It simply draws the good old COLOR_BTNFACE as the background of those controls, as shown in the following example:

As you can see this is totally wrong

The first idea: Property sheets

So what are we going to do about this? Well first of all, there just must be a way to correctly draw this as there are certain applications such as Internet Explorer which draw it the right way. After a bit of investigation, I figured out they are using property sheets. So I decided to try a property sheet myself and to my surprise it's drawing correctly using a property sheet. However, it is just drawing correctly with the one and only tab control provided by the property sheet itself! Hmmm, so that's not too nice either and besides, sometimes property sheets are just not what you want and it's much too much work to configure one properly. I attempted to figure out how they got the property sheet to draw correctly, but unfortunately I could not find anything. If however you don't need anything fancy or custom, a property sheet might just do for you.

The second idea: EnableThemeDialogTexture

Well, there must be some way Windows is drawing that nice tab control background, so I decided to take a look in the UxTheme library. I found this function in there and it did show some interesting effects. This function enables you to automatically draw a background similar to the tab background on your dialog. So why doesn't this work? Well, it draws the background on the dialog itself. So everything that actually -should- be COLOR_BTNFACE is now drawn as if it has the background of a tab control as well, plus the background of the children are not aligned correctly, so you still see an ugly dialog.

The third idea: Transparency

You ever noticed the property 'Transparent' on your static controls? Well, if you did, you might also have noticed that it simply doesn't work... This doesn't mean however that we can't make it work. We actually can! How, you ask? Well, by subclassing the WM_CTLCOLORSTATIC message. This message gets send to the dialog box every time a child window requests the background brush to be drawn with. Well, that sounds perfect doesn't it? If we could just return a transparent brush, it's all fine and you know what? There is just such a thing! If we use the following code, it actually -seems- to work:

LRESULT OnCtlColor(UINT /* uMsg */, WPARAM wParam, 
       LPARAM /* lParam */, BOOL& /* bHandled */) throw()
    // Set the background mode to transparent
    ::SetBkMode((HDC)(wParam), TRANSPARENT);

    // Return the brush return 

So, if we run the program, the static controls actually work. But, and there's always a but isn't there... The radio button and checkbox display black backgrounds! (On this screenshot the radio button even keeps its grey background somehow):

Huh? A little hard to read this way

So, now what? Well, I did some extensive searching on the Internet for this problem and I actually found some sites mentioning this problem, but no one really offered a solution besides using a property sheet. So what I did next was to look how it is achieved to display bitmaps as background of a dialog while being transparent. I got a sample of Microsoft which gave me a very nice, al-be-it a sort of 'hack' idea.

The last and final idea: We use a bitmap as background

What? I hear you say. We use a bitmap? So how are we doing that? Well, I answer, we simply take a sort of screenshot from the tab control at creation and every time it resizes, and we have the perfect background bitmap for the transparent controls. Interesting idea, so how does it work? What we desire is to get an image of the background and the background only of the tab control. There are a couple of requirements for this idea:

  1. We want an image of the background of the tab control and just the background and not the children, as oddities might occur if, for instance, a child control changes its caption.
  2. We need to create a brush of this bitmap and we don't want to recreate it all the time, because it's rather inefficient. So we only update this brush when the tab control gets created and when the tab control gets resized.
  3. Now we have the background brush, it needs to be aligned for each control, else we see the top left corner of the tab control as the background of each child.
  4. It would be nice to have an easy way of implementing this on a dialog box without having to copy-paste anything to recreate the effect.
  5. As noted by A sleepy one, the code controls don't display correctly with earlier versions of Windows, so we need to find a way to only use it if the themes are enabled.

Well, the solution is near. I created a template class which does the following. It intercepts the WM_INITDIALOG message and subclasses the tab control. It calls the UpdateBackgroundBrush() function each time the tab control resizes and after we've subclassed it for the initial state. The function looks like this:

void UpdateBackgroundBrush() throw()
    HMODULE hinstDll;

    // Check if the application is themed
    hinstDll = ::LoadLibrary(_T("UxTheme.dll"));
    if (hinstDll)
        typedef BOOL (*ISAPPTHEMEDPROC)();
        ISAPPTHEMEDPROC pIsAppThemed;
        pIsAppThemed = 
          (ISAPPTHEMEDPROC) ::GetProcAddress(hinstDll, "IsAppThemed");

            m_bThemeActive = pIsAppThemed();


    // Destroy old brush
    if (m_hBrush)

    m_hBrush = NULL;

    // Only do this if the theme is active
    if (m_bThemeActive)
        RECT rc;

        // Get tab control dimensions

        // Get the tab control DC
        HDC hDC = m_wndTab.GetDC();

        // Create a compatible DC
        HDC hDCMem = ::CreateCompatibleDC(hDC);
        HBITMAP hBmp = ::CreateCompatibleBitmap(hDC, 
               rc.right - rc.left, rc.bottom -;
        HBITMAP hBmpOld = (HBITMAP)(::SelectObject(hDCMem, hBmp));

        // Tell the tab control to paint in our DC
        m_wndTab.SendMessage(WM_PRINTCLIENT, (WPARAM)(hDCMem), 

        // Create a pattern brush from the bitmap selected in our DC
        m_hBrush = ::CreatePatternBrush(hBmp);

        // Restore the bitmap
        ::SelectObject(hDCMem, hBmpOld);

        // Cleanup

So, what exactly do I do? It's quite simple. First of all, I dynamically load the IsAppThemed function so this code is compatible with earlier versions of Windows. Per default, the m_bThemeActive is set to false. If the function can get loaded, it executes it to verify if the themes are active. In case the themes are active, I create a memory dc and a bitmap in the memory dc of the same size as the tab control. When I've created the memory dc, we send the WM_PRINTCLIENT message to the tab control. What does this message do? Well, we can send this message to the tab control and it will draw itself inside the dc specified by us. Pretty nifty message huh? The biggest advantage when we would use BitBlt to copy the bitmap from the tab control to our bitmap is that this way we don't include the children. Well, after that, we simply create a brush from the bitmap and clean up the resources we used.

So now we have the brush to use, but how do we use it for the children of the tab control? Well, we go back to our third idea and subclass the WM_CTLCOLORSTATIC message. The handler for this message will look like this:

LRESULT OnCtlColor(UINT /* uMsg */, WPARAM wParam, 
     LPARAM lParam, BOOL& /* bHandled */) throw()
    if (m_bThemeActive)
        RECT rc;

        // Set the background mode to transparent
        ::SetBkMode((HDC)(wParam), TRANSPARENT);

        // Get the controls window dimensions
        ::GetWindowRect((HWND)(lParam), &rc);

        // Map the coordinates to coordinates with the upper
        // left corner of dialog control as base
        ::MapWindowPoints(NULL, m_wndTab, (LPPOINT)(&rc), 2);

        // Adjust the position of the brush for this control
        // (else we see the top left of the brush as background)
        ::SetBrushOrgEx((HDC)(wParam), -rc.left,, NULL);

        // Return the brush
        return (LRESULT)(m_hBrush);

    return FALSE;

It looks a lot like the one from my third idea. It sets the background mode to transparent again and it returns the created brush from the UpdateBackgroundBrush() function. There is a little added code though, to make sure the brush is aligned correctly so we don't see the top left of the tab control as the background of the children. Also, it checks if the application is themed so it doesn't perform unnecessary tasks. So how does this look in reality?

Woah! It works!

Well, as you can see it works! It's a rather dirty way of solving an ugly glitch :-) But it works and this far I haven't seen another solution just yet. So how do you use this class in your code? It's fairly easy:

class CSomeDialog: public CThemedDialog<CSomeDialog, IDD_DIALOG1, IDC_TAB1>
    typedef CThemedDialog<CSomeDialog, IDD_DIALOG1, IDC_TAB1> SUPERCLASS;


This is all you need to do. In case you don't want to subclass the dialog, you can drop the message map all together. In case you do, you have to remind to chain the alternate message map as well. This alternate message map is used for subclassing the tab control. As parameters for the template you have to pass the class itself, the resource identifier of the dialog box and the resource identifier of the tab control inside the dialog box. It only supports one tab control however, and if you desire to support more, you'll have to adjust the code a bit. Enjoy!

Revision History

  • 29-01-2004: Updated the example and screenshots to show the difference between edit controls as well and the application detects if it's themed or not so you can use the same code on earlier versions of Windows. Also updated the article to reflect the changes in the example code.
  • 30-01-2004: Added Win32 code and sample.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Luuk Weltevreden
Web Developer
Netherlands Netherlands
No Biography provided

Comments and Discussions

AnswerJust simple using this code... Pin
Xenophōne25-Mar-14 19:21
professionalXenophōne25-Mar-14 19:21 
QuestionDialog Windows and Themes matching. Can you help? Pin
k hinds17-Jul-08 20:22
Memberk hinds17-Jul-08 20:22 
GeneralCompiler error with VC2005 Pin
briancm10-Oct-07 10:29
Memberbriancm10-Oct-07 10:29 
GeneralRe: Compiler error with VC2005 Pin
briancm10-Oct-07 11:04
Memberbriancm10-Oct-07 11:04 
GeneralA different approach ( works for group boxes too.. ) Pin
Steve Johnson (Sven)18-May-07 11:15
MemberSteve Johnson (Sven)18-May-07 11:15 
GeneralThis is the real working solution to this problem !! Pin
jeffreycui@hotmail.com29-Apr-08 11:37
Memberjeffreycui@hotmail.com29-Apr-08 11:37 
GeneralRe: A different approach ( works for group boxes too.. ) Pin
pscholl21-Aug-08 2:56
Memberpscholl21-Aug-08 2:56 
GeneralRe: A different approach ( works for group boxes too.. ) Pin
rotalume9-Apr-10 1:29
Memberrotalume9-Apr-10 1:29 
GeneralA problem remains Pin
Niklas L1-Sep-05 6:15
MemberNiklas L1-Sep-05 6:15 
GeneralNice work Pin
nicoster11-Aug-05 17:57
Membernicoster11-Aug-05 17:57 
Generala small suggestion Pin
vanqru28-Jun-05 23:02
Membervanqru28-Jun-05 23:02 
GeneralA faster way Pin
Anonymous31-Oct-04 5:16
MemberAnonymous31-Oct-04 5:16 
cybershield12-Oct-04 1:52
Membercybershield12-Oct-04 1:52 
Generaluse DrawThemeParentBackground Pin
biondello17-Aug-04 8:48
Memberbiondello17-Aug-04 8:48 
GeneralExcellent idea! Pin
lajkonik10-Mar-06 20:00
Memberlajkonik10-Mar-06 20:00 
GeneralRe: use DrawThemeParentBackground Pin
agurtovoy24-Mar-08 12:18
Memberagurtovoy24-Mar-08 12:18 
GeneralNice!! Pin
drudru17-May-04 15:03
Memberdrudru17-May-04 15:03 
GeneralRe: Nice!! Pin
chuvv26-Dec-06 13:52
Memberchuvv26-Dec-06 13:52 
GeneralIsAppThemed Pin
Steve Thresher16-May-04 23:46
MemberSteve Thresher16-May-04 23:46 
GeneralRe: IsAppThemed Pin
KHDev4u26-Jul-05 13:16
MemberKHDev4u26-Jul-05 13:16 
GeneralRe: IsAppThemed Pin
Steve Thresher26-Jul-05 13:31
MemberSteve Thresher26-Jul-05 13:31 
GeneralExcellent Article Pin
Steve Thresher13-May-04 12:03
MemberSteve Thresher13-May-04 12:03 
GeneralRe: Excellent Article Pin
Logan20005-Dec-04 20:30
sussLogan20005-Dec-04 20:30 
GeneralGroupbox Pin
chinhster11-Mar-04 5:30
Memberchinhster11-Mar-04 5:30 
GeneralRe: Groupbox Pin
Luuk Weltevreden11-Mar-04 5:47
MemberLuuk Weltevreden11-Mar-04 5:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.