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

Yet Another Transparent Static Control

By , 14 Dec 2004
 

Sample Image - TransparentStatic.jpg

Introduction

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();
   Invalidate();
   UpdateWindow();
   return Result;
}

HBRUSH CTransparentStatic2::CtlColor(CDC* pDC, UINT /*nCtlColor*/)
{
   pDC->SetBkMode(TRANSPARENT);
   return (HBRUSH)GetStockObject(NULL_BRUSH);
}

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

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

      MemDC.BitBlt(0,0,Rect.Width(),Rect.Height(),pDC,Rect.left,Rect.top,SRCCOPY);

      MemDC.SelectObject(pOldBmp);

      pParent->ReleaseDC(pDC);
   }
   else //copy what we copied off the parent the first time back onto the parent
   {
      CRect Rect;
      GetClientRect(Rect);
      CDC MemDC;
      MemDC.CreateCompatibleDC(pDC);
      CBitmap *pOldBmp = MemDC.SelectObject(&m_Bmp);
      pDC->BitBlt(0,0,Rect.Width(),Rect.Height(),&MemDC,0,0,SRCCOPY);
      MemDC.SelectObject(pOldBmp);
   }

   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;
   GetWindowRect(&Rect);
   GetParent()->ScreenToClient(&Rect);
   GetParent()->InvalidateRect(&Rect);
   GetParent()->UpdateWindow();
   return Result;

}

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

{
   pDC->SetBkMode(TRANSPARENT);
   return (HBRUSH)GetStockObject(NULL_BRUSH);

}

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

License

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

About the Author

Ali Rafiee
Architect
United States United States
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5 PinmemberAndrehPoffo9 Apr '13 - 10:25 
Questiontrans dymamic text static PinmemberMember 50583930 Mar '12 - 8:45 
GeneralPls pls help- this method not working for me on MS VS2005 for windows mobile PinmemberVanita Jain27 Aug '09 - 12:53 
GeneralRe: Pls pls help- this method not working for me on MS VS2005 for windows mobile PinmemberAli Rafiee28 Aug '09 - 4:49 
GeneralTransparent Edit Control PinmemberAndrew Phillips28 Jan '07 - 20:21 
GeneralRe: Transparent Edit Control PinmemberAli Rafiee29 Jan '07 - 4:53 
AnswerRe: Transparent Edit Control PinmemberAli Rafiee8 Jun '07 - 10:22 
GeneralHOLLOW_BRUSH is enough PinmemberVladmiir18 Apr '06 - 0:50 
AnswerRe: HOLLOW_BRUSH is enough PinmemberAli Rafiee18 Apr '06 - 6:22 
GeneralRe: HOLLOW_BRUSH is NOT enough PinmemberMoak14 Mar '08 - 2:23 
QuestionTransparency when dialog is not showed then showed ... Pinmemberouquoi16 Feb '06 - 5:02 
Hi,
Thanks for your Transparent Static.
But i have a problem with my application.
 
I build a CDialog dynamicaly with all the control not showed until all the control are created.
And when i show the dialog with all the control the background of the static is the image of my VC++ because the bmp background is calculate when my dialog is not showed.
 
Have you any idea how to get the right background or force to paint when the dialog is visible.
 
Thanks.

AnswerRe: Transparency when dialog is not showed then showed ... PinmemberAli Rafiee16 Feb '06 - 9:59 
GeneralRe: Transparency when dialog is not showed then showed ... Pinmemberouquoi16 Feb '06 - 21:47 
GeneralRe: Transparency when dialog is not showed then showed ... PinmemberAli Rafiee17 Feb '06 - 4:33 
GeneralRe: Transparency when dialog is not showed then showed ... Pinmemberouquoi19 Feb '06 - 21:25 
AnswerRe: Transparency when dialog is not showed then showed ... PinmemberDouglas R. Keesler6 Mar '06 - 17:53 
AnswerRe: Transparency when dialog is not showed then showed ... Pinmembercaoxiang13 Mar '06 - 2:35 
AnswerRe: Transparency when dialog is not showed then showed ... Pinmembercaoxiang13 Mar '06 - 2:37 
QuestionTransparent Radio Button? PinmemberNobser1 Feb '06 - 8:32 
AnswerRe: Transparent Radio Button? PinmemberNobser1 Feb '06 - 21:17 
Questionwhy bitmap static or static1 can show up? PinmemberAnonymousBabe@usa.net20 Sep '05 - 17:46 
AnswerRe: why bitmap static or static1 can show up? PinmemberAliRafiee21 Sep '05 - 7:55 
GeneralRe: why bitmap static or static1 can show up? PinmemberAnonymousBabe@usa.net22 Sep '05 - 15:16 
GeneralRe: why bitmap static or static1 can show up? PinmemberAliRafiee23 Sep '05 - 5:46 
QuestionTransparent ListBox? Pinmemberiesoft10 Aug '05 - 10:39 
AnswerRe: Transparent ListBox? PinmemberAliRafiee10 Aug '05 - 11:17 
GeneralRe: Transparent ListBox? PinmemberAliRafiee11 Aug '05 - 6:30 
GeneralProblem with static method PinmemberCeri29 Jun '05 - 23:44 
GeneralRe: Problem with static method PinmemberAliRafiee1 Jul '05 - 5:35 
AnswerRe: Problem with static method PinmemberDouglas R. Keesler6 Mar '06 - 18:24 
GeneralDoes not work when control is created via Create() method PinmemberRalph5 Apr '05 - 9:37 
GeneralRe: Does not work when control is created via Create() method PinmemberAliRafiee5 Apr '05 - 9:51 
GeneralRe: Does not work when control is created via Create() method Pinmemberavatar3d23 Jun '05 - 15:58 
GeneralRe: Does not work when control is created via Create() method PinmemberAliRafiee21 Sep '05 - 8:00 
General[OFFTOPIC] About first jpg PinsussKlimmtom21 Dec '04 - 21:03 
GeneralRe: [OFFTOPIC] About first jpg Pinmemberarmentage25 Apr '05 - 5:41 
GeneralShadow PinmemberJaded Hobo21 Dec '04 - 3:34 
GeneralRe: Shadow PinmemberAliRafiee21 Dec '04 - 4:13 
QuestionDo you have a VC6 version? PinmemberWREY15 Dec '04 - 8:51 
AnswerRe: Do you have a VC6 version? PinmemberAliRafiee15 Dec '04 - 9:26 

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 14 Dec 2004
Article Copyright 2004 by Ali Rafiee
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid