Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / MFC
Article

Transparent Static Text In Dialogs

Rate me:
Please Sign up or sign in to vote.
4.39/5 (25 votes)
4 Jul 20043 min read 193.8K   7.6K   61   29
A simple way to make static text controls draw transparently over the dialog background.

Sample Image - TransWiz.gif

Introduction

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.

History

  • 1.0 -15 Jul 2004 - first version.

License

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


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

Comments and Discussions

 
GeneralMy vote of 5 Pin
k77721-Oct-10 11:10
k77721-Oct-10 11:10 
GeneralYou saved my day!!! Pin
k77721-Oct-10 11:09
k77721-Oct-10 11:09 
QuestionWhat if there are overlappinng bitmaps below the static text box Pin
amephraim27-May-10 0:27
amephraim27-May-10 0:27 
GeneralVC++ question Pin
ibrahim_8410-Dec-07 19:00
ibrahim_8410-Dec-07 19:00 
GeneralA little remark Pin
toxcct9-Jan-07 11:55
toxcct9-Jan-07 11:55 
GeneralRe: A little remark Pin
Roman Ru30-Jul-10 6:14
Roman Ru30-Jul-10 6:14 
GeneralSetWindowText problem Pin
Ali Rafiee1-Jul-05 5:44
Ali Rafiee1-Jul-05 5:44 
GeneralRe: SetWindowText problem Pin
Dana Gregory2-Jul-05 13:32
Dana Gregory2-Jul-05 13:32 
GeneralRe: SetWindowText problem Pin
toxcct9-Jan-07 11:42
toxcct9-Jan-07 11:42 
GeneralRe: SetWindowText problem Pin
Amit R. Sharma3-Mar-13 16:19
Amit R. Sharma3-Mar-13 16:19 
GeneralRe: SetWindowText problem Pin
Anonymous27-Jul-05 21:04
Anonymous27-Jul-05 21:04 
GeneralRe: SetWindowText problem Pin
Ali Rafiee28-Jul-05 4:59
Ali Rafiee28-Jul-05 4:59 
QuestionHow to paint the CTreeCtrl transparently? Pin
Martial Spirit11-May-05 0:20
Martial Spirit11-May-05 0:20 
GeneralOnly transparent with static initialization Pin
Vince Ricci27-Sep-04 0:00
Vince Ricci27-Sep-04 0:00 
GeneralRe: Only transparent with static initialization Pin
Dana Gregory27-Sep-04 2:05
Dana Gregory27-Sep-04 2:05 
When you do this, the old text doesn't get erased because of the Nuill Brush being used for the background. The new text just gets or'd over it and you end up with garbage.

The workaround I am using in my application is to Hide the static item, change the text and then Show the item.
Questionfor c# anybody has sample code? Pin
temp555613-Jul-04 13:10
temp555613-Jul-04 13:10 
AnswerRe: for c# anybody has sample code? Pin
Anonymous16-Jul-04 14:18
Anonymous16-Jul-04 14:18 
AnswerRe: for c# anybody has sample code? Pin
Anonymous16-Jul-04 14:20
Anonymous16-Jul-04 14:20 
GeneralCheck box behaviour Pin
StickJock8-Jul-04 15:03
StickJock8-Jul-04 15:03 
GeneralRe: Check box behaviour Pin
Dana Gregory9-Jul-04 5:00
Dana Gregory9-Jul-04 5:00 
GeneralRe: Check box behaviour Pin
Nicolas MEURET19-Nov-04 3:07
Nicolas MEURET19-Nov-04 3:07 
GeneralRe: Check box behaviour Pin
Member 31748432-Dec-08 19:34
Member 31748432-Dec-08 19:34 
GeneralDoesn't work with XP Style Pin
Vitaly Tomilov7-Jun-05 0:36
Vitaly Tomilov7-Jun-05 0:36 
GeneralRe: Check box behaviour Pin
CALIFF231-Jul-08 10:24
CALIFF231-Jul-08 10:24 
GeneralAlternative Pin
c2j27-Jul-04 23:40
c2j27-Jul-04 23:40 

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.