Click here to Skip to main content
Click here to Skip to main content

Transparent Static Text In Dialogs

, 4 Jul 2004
Rate this:
Please Sign up or sign in to vote.
A simple way to make static text controls draw transparently over the dialog background.

Sample Image - TransWiz.gif


Recently, I was implementing a PropertySheet based wizard, and part of the graphic design was to place a bitmap as the window background and then draw the text and other controls over that. I found a few examples of using a bitmap as a dialog background - the best of which simply involved overriding the default WM_ERASEBKGND behavior.

That worked fine, but of course, the Static text controls were still being drawn on their beige backgrounds. I found a couple of ideas about drawing transparent text, but they generally involved overriding WM_PAINT and doing all the text drawing yourself. I was hoping for something simpler.

There are numerous articles about overriding WM_CTLCOLOR, and they generally discuss using this as a way to change the text color or the background color. I was put off for a long time by the name 'WM_CTLCOLOR' thinking that it was just about color. However, you can change several characteristics of the drawing environment here just before a control is drawn.

It is possible to call SetBkMode and set the background mode to TRANSPARENT which keeps the text drawing routines from erasing before drawing text. This is not quite enough however because when a Static control is drawn, the entire control background is first painted with the HBRUSH returned by OnCtlColor. Fortunately, having OnCtlColor return the NULL_BRUSH takes care of this as well.

Using the code

This example implements a PropertySheet based wizard. The same techniques would apply to a Dialog.

To change the CStatic text behavior to transparent drawing requires adding one message handler with four lines of code.

I create a sub-class of CPropertyPage (for a regular dialog, you would create a sub-class of CDialog). Then I use ClassWizard to create a handler for WM_CTLCOLOR. In that handler, OnCtlColor, I check the nCtlColor parameter, and if it shows that we are about to draw a Static control (CTLCOLOR_STATIC) then I call SetBkMode to change the DC mode to TRANSPARENT. I also get a handle to the stock NULL_BRUSH object and return that as the function result. Then Windows takes care of all the text drawing itself.

You can change a number of things about the DC in OnCtlColor. All of the various CDC functions are available such as SetTextColor which allows you to change the text color. Of special note is the ability to use CDC::SelectObject which allows you to change the Pen, Brush, Font, Bitmap and Region. The ability to modify the font here is especially useful in working with PropertySheets because there is no way to set the face, size, or style of the font used (PropertySheets always use MS Shell).

This example also implements using a bitmap for the dialog background. This is done by using ClassWizard to create a handler for WM_ERASEBKGND in the CPropertyPage (CDialog) sub-class. The OnEraseBkgrnd routine then fills the window with the bitmap instead of painting with the background brush.


  • 1.0 -15 Jul 2004 - first version.


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

Web Developer
United States United States
Software engineer working on commercial applications used in color management.

Comments and Discussions

GeneralMy vote of 5 Pinmemberk77721-Oct-10 12:10 
GeneralYou saved my day!!! Pinmemberk77721-Oct-10 12:09 
QuestionWhat if there are overlappinng bitmaps below the static text box Pinmemberamephraim27-May-10 1:27 
GeneralVC++ question Pinmemberansif_pi10-Dec-07 20:00 
GeneralA little remark Pinmvptoxcct9-Jan-07 12:55 
GeneralRe: A little remark PinmemberRoman Ru30-Jul-10 7:14 
GeneralSetWindowText problem PinmemberAliRafiee1-Jul-05 6:44 
GeneralRe: SetWindowText problem Pinsussdanamac2-Jul-05 14:32 
GeneralRe: SetWindowText problem Pinmvptoxcct9-Jan-07 12:42 
GeneralRe: SetWindowText problem PinmemberAmit R. Sharma3-Mar-13 17:19 
GeneralRe: SetWindowText problem PinsussAnonymous27-Jul-05 22:04 
GeneralRe: SetWindowText problem PinmemberAliRafiee28-Jul-05 5:59 
QuestionHow to paint the CTreeCtrl transparently? PinmemberMartial Spirit11-May-05 1:20 
GeneralOnly transparent with static initialization Pinsussvsix27-Sep-04 1:00 
GeneralRe: Only transparent with static initialization Pinsussdanamac27-Sep-04 3:05 
Questionfor c# anybody has sample code? Pinmembertemp555613-Jul-04 14:10 
AnswerRe: for c# anybody has sample code? PinsussAnonymous16-Jul-04 15:18 
AnswerRe: for c# anybody has sample code? PinsussAnonymous16-Jul-04 15:20 
GeneralCheck box behaviour PinmemberStickJock8-Jul-04 16:03 
GeneralRe: Check box behaviour Pinsussdanamac9-Jul-04 6:00 
GeneralRe: Check box behaviour PinmemberNicolas MEURET19-Nov-04 4:07 
GeneralRe: Check box behaviour PinmemberMember 31748432-Dec-08 20:34 
GeneralDoesn't work with XP Style PinmemberVitalyTomilov7-Jun-05 1:36 
GeneralRe: Check box behaviour Pinmembercaliff_usa31-Jul-08 11:24 
GeneralAlternative Pinmemberc2j28-Jul-04 0:40 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150331.1 | Last Updated 5 Jul 2004
Article Copyright 2004 by danamac
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid