Click here to Skip to main content
13,247,891 members (42,726 online)
Click here to Skip to main content
Add your own
alternative version


51 bookmarked
Posted 14 Dec 2004

Yet Another Transparent Static Control

, 14 Dec 2004
Rate this:
Please Sign up or sign in to vote.
Two other methods of making a transparent static control.

Sample Image - TransparentStatic.jpg


There are many ways to develop a transparent static control. I like two approaches in particular, because they are simple and they get the job done. When you think about placing a static control with a transparent background on your window, the one important thing you have to consider is, whether the background is static or dynamic. The problem of having a static control changes depending on what is displayed in the background.

Method 1

If the background is static, then the best approach is to make a copy of the area on the parent window behind the static control before it is drawn for the first time, and then simply copy the image back to the parent every time the static control needs to be redrawn. The advantage of this approach is its flicker free display every time the static control is changed.

LRESULT CTransparentStatic2::OnSetText(WPARAM wParam,LPARAM lParam)

   LRESULT Result = Default();
   return Result;

HBRUSH CTransparentStatic2::CtlColor(CDC* pDC, UINT /*nCtlColor*/)
   return (HBRUSH)GetStockObject(NULL_BRUSH);

BOOL CTransparentStatic2::OnEraseBkgnd(CDC* pDC)
   if (m_Bmp.GetSafeHandle() == NULL)
      CRect Rect;
      CWnd *pParent = GetParent();
      pParent->ScreenToClient(&Rect); //convert our corrdinates to our parents

      //copy what's on the parents at this point
      CDC *pDC = pParent->GetDC();
      CDC MemDC;
      CBitmap *pOldBmp = MemDC.SelectObject(&m_Bmp);



   else //copy what we copied off the parent the first time back onto the parent
      CRect Rect;
      CDC MemDC;
      CBitmap *pOldBmp = MemDC.SelectObject(&m_Bmp);

   return TRUE;

Method 2

The other approach comes in handy when the parent window's background is dynamic. If the parent window's background is constantly changing then the above approach will not look too nice. So instead of all the copying, what the static control should do is invalidate the area of the parent where the control resides every time its text is changed. This method is much simpler than the one before, but it could flicker if the text is changed at a rapid rate.

LRESULT CTransparentStatic::OnSetText(WPARAM wParam,LPARAM lParam)

   LRESULT Result = Default();
   CRect Rect;
   return Result;


HBRUSH CTransparentStatic::CtlColor(CDC* pDC, UINT /*nCtlColor*/)

   return (HBRUSH)GetStockObject(NULL_BRUSH);


Keep an eye out for a Transparent Edit control and Transparent Listbox.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ali Rafiee
United States United States
Ali Rafiee has been developing windows applications using C++ since 1991, and he hasn't looked back since. Ali has been a software development consultant for must of his career, but he has finally settled down and has been working for an educational software company since 2000. While he is not working, he is either learning C#, flying airplanes, playing with his daughter, or answering peoples question on newsgroups, he finds that to be a great learning tool for himself.

Ali is also a Microsoft Visual C++ MVP.

You may also be interested in...


Comments and Discussions

QuestionAny Problem With This Version Of Your Code? Pin
Ali Zafer Gürbüz13-Mar-16 18:36
memberAli Zafer Gürbüz13-Mar-16 18:36 
QuestionHow would this work with 32-bit drawing (GDI+) ? Pin
_Future_9-Oct-15 14:00
member_Future_9-Oct-15 14:00 
Suggestionwhen failed to erase previous Text, like duplicated texts Pin
Member 326465925-Mar-14 17:41
memberMember 326465925-Mar-14 17:41 
GeneralMy vote of 5 Pin
AndrehPoffo9-Apr-13 11:25
memberAndrehPoffo9-Apr-13 11:25 
Questiontrans dymamic text static Pin
Member 50583930-Mar-12 9:45
memberMember 50583930-Mar-12 9:45 
GeneralPls pls help- this method not working for me on MS VS2005 for windows mobile Pin
Vanita Jain27-Aug-09 13:53
memberVanita Jain27-Aug-09 13:53 
GeneralRe: Pls pls help- this method not working for me on MS VS2005 for windows mobile Pin
Ali Rafiee28-Aug-09 5:49
memberAli Rafiee28-Aug-09 5:49 
GeneralTransparent Edit Control Pin
Andrew Phillips28-Jan-07 21:21
memberAndrew Phillips28-Jan-07 21:21 
GeneralRe: Transparent Edit Control Pin
Ali Rafiee29-Jan-07 5:53
memberAli Rafiee29-Jan-07 5:53 
AnswerRe: Transparent Edit Control Pin
Ali Rafiee8-Jun-07 11:22
memberAli Rafiee8-Jun-07 11:22 
GeneralHOLLOW_BRUSH is enough Pin
Vladmiir18-Apr-06 1:50
memberVladmiir18-Apr-06 1:50 
AnswerRe: HOLLOW_BRUSH is enough Pin
Ali Rafiee18-Apr-06 7:22
memberAli Rafiee18-Apr-06 7:22 
GeneralRe: HOLLOW_BRUSH is NOT enough Pin
Moak14-Mar-08 3:23
memberMoak14-Mar-08 3:23 
QuestionTransparency when dialog is not showed then showed ... Pin
ouquoi16-Feb-06 6:02
memberouquoi16-Feb-06 6:02 
AnswerRe: Transparency when dialog is not showed then showed ... Pin
Ali Rafiee16-Feb-06 10:59
memberAli Rafiee16-Feb-06 10:59 
The problem her is that when the Static control is created it will receive a WM_ERASEBKGND message where it is getting the background it needs in order to draw itself..
There are three ways you can get around this problem.

1. Is to user CTransparentStatic instead of CTransparentStatic2 (I should have named these classes better). This class assumes that the background of the Parent is dynamically changing. So right after you show your controls, call the Static controls SetWindowText, and things will show up fine.
CString Text;

2. You can add a method to the CTransparnetStatic2 class to clear it's cached background so that it is in an uninitialized state. Call that method right before you display your dialog box.

3. Instead of not showing the dialog, you can move it off screen, once everything is setup move it back to it's orginal place.

GeneralRe: Transparency when dialog is not showed then showed ... Pin
ouquoi16-Feb-06 22:47
memberouquoi16-Feb-06 22:47 
GeneralRe: Transparency when dialog is not showed then showed ... Pin
Ali Rafiee17-Feb-06 5:33
memberAli Rafiee17-Feb-06 5:33 
GeneralRe: Transparency when dialog is not showed then showed ... Pin
ouquoi19-Feb-06 22:25
memberouquoi19-Feb-06 22:25 
AnswerRe: Transparency when dialog is not showed then showed ... Pin
Douglas R. Keesler6-Mar-06 18:53
memberDouglas R. Keesler6-Mar-06 18:53 
AnswerRe: Transparency when dialog is not showed then showed ... Pin
caoxiang13-Mar-06 3:35
membercaoxiang13-Mar-06 3:35 
AnswerRe: Transparency when dialog is not showed then showed ... Pin
caoxiang13-Mar-06 3:37
membercaoxiang13-Mar-06 3:37 
QuestionTransparent Radio Button? Pin
Nobser1-Feb-06 9:32
memberNobser1-Feb-06 9:32 
AnswerRe: Transparent Radio Button? Pin
Nobser1-Feb-06 22:17
memberNobser1-Feb-06 22:17 
Questionwhy bitmap static or static1 can show up? Pin
AnonymousBabe@usa.net20-Sep-05 18:46
memberAnonymousBabe@usa.net20-Sep-05 18:46 
AnswerRe: why bitmap static or static1 can show up? Pin
AliRafiee21-Sep-05 8:55
memberAliRafiee21-Sep-05 8:55 

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
Web02 | 2.8.171114.1 | Last Updated 14 Dec 2004
Article Copyright 2004 by Ali Rafiee
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid