Click here to Skip to main content
13,287,116 members (45,968 online)
Click here to Skip to main content
Add your own
alternative version


61 bookmarked
Posted 4 Jul 2004

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.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
k77721-Oct-10 12:10
memberk77721-Oct-10 12:10 
Simple and clear
GeneralYou saved my day!!! Pin
k77721-Oct-10 12:09
memberk77721-Oct-10 12:09 
QuestionWhat if there are overlappinng bitmaps below the static text box Pin
amephraim27-May-10 1:27
memberamephraim27-May-10 1:27 
GeneralVC++ question Pin
ansif_pi10-Dec-07 20:00
memberansif_pi10-Dec-07 20:00 
GeneralA little remark Pin
toxcct9-Jan-07 12:55
mvptoxcct9-Jan-07 12:55 
GeneralRe: A little remark Pin
Roman Ru30-Jul-10 7:14
memberRoman Ru30-Jul-10 7:14 
GeneralSetWindowText problem Pin
AliRafiee1-Jul-05 6:44
memberAliRafiee1-Jul-05 6:44 
GeneralRe: SetWindowText problem Pin
danamac2-Jul-05 14:32
sussdanamac2-Jul-05 14:32 
GeneralRe: SetWindowText problem Pin
toxcct9-Jan-07 12:42
mvptoxcct9-Jan-07 12:42 
GeneralRe: SetWindowText problem Pin
Amit R. Sharma3-Mar-13 17:19
memberAmit R. Sharma3-Mar-13 17:19 
GeneralRe: SetWindowText problem Pin
Anonymous27-Jul-05 22:04
sussAnonymous27-Jul-05 22:04 
GeneralRe: SetWindowText problem Pin
AliRafiee28-Jul-05 5:59
memberAliRafiee28-Jul-05 5:59 
QuestionHow to paint the CTreeCtrl transparently? Pin
Martial Spirit11-May-05 1:20
memberMartial Spirit11-May-05 1:20 
GeneralOnly transparent with static initialization Pin
vsix27-Sep-04 1:00
sussvsix27-Sep-04 1:00 
GeneralRe: Only transparent with static initialization Pin
danamac27-Sep-04 3:05
sussdanamac27-Sep-04 3:05 
Questionfor c# anybody has sample code? Pin
temp555613-Jul-04 14:10
membertemp555613-Jul-04 14:10 
AnswerRe: for c# anybody has sample code? Pin
Anonymous16-Jul-04 15:18
sussAnonymous16-Jul-04 15:18 
AnswerRe: for c# anybody has sample code? Pin
Anonymous16-Jul-04 15:20
sussAnonymous16-Jul-04 15:20 
GeneralCheck box behaviour Pin
StickJock8-Jul-04 16:03
memberStickJock8-Jul-04 16:03 
GeneralRe: Check box behaviour Pin
danamac9-Jul-04 6:00
sussdanamac9-Jul-04 6:00 
GeneralRe: Check box behaviour Pin
Nicolas MEURET19-Nov-04 4:07
memberNicolas MEURET19-Nov-04 4:07 
GeneralRe: Check box behaviour Pin
Member 31748432-Dec-08 20:34
memberMember 31748432-Dec-08 20:34 
GeneralDoesn't work with XP Style Pin
VitalyTomilov7-Jun-05 1:36
memberVitalyTomilov7-Jun-05 1:36 
GeneralRe: Check box behaviour Pin
califf_usa31-Jul-08 11:24
membercaliff_usa31-Jul-08 11:24 
GeneralAlternative Pin
c2j28-Jul-04 0:40
memberc2j28-Jul-04 0:40 
GeneralRe: Alternative Pin
talbot23-Feb-07 5:14
membertalbot23-Feb-07 5:14 
GeneralRe: Alternative Pin
cristitomi21-Mar-07 22:25
membercristitomi21-Mar-07 22:25 
GeneralRe: Alternative [modified] Pin
ashwinjam30-Jun-08 22:28
memberashwinjam30-Jun-08 22:28 
GeneralOn Windows CE... Pin
Johann Gerell5-Jul-04 21:56
memberJohann Gerell5-Jul-04 21:56 

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.

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