Click here to Skip to main content
15,910,603 members
Articles / Desktop Programming / MFC
Article

Yet Another Transparent Static Control

Rate me:
Please Sign up or sign in to vote.
4.67/5 (24 votes)
14 Dec 2004CPOL1 min read 214.3K   7.7K   54   43
Two other methods of making a transparent static control.

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)


Written By
Architect
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.

Comments and Discussions

 
GeneralRe: why bitmap static or static1 can show up? Pin
AnonymousBabe@usa.net22-Sep-05 15:16
AnonymousBabe@usa.net22-Sep-05 15:16 
AliRafiee wrote:
That has to do with the ZOrder of the controls. The controls with higher ZOrder will show up on top of controls with a lower ZOrder.

after a few try, I think so, but I have not gotten the inferior from the tries.
1)

I new a project with two statics, one Labled "aaaa",
another one labled "bbbbbbbbbbbbbbbbb",
and the "aaaa" is bigger than "bbbbbbbbbbbbbbbbbbbbb" in size.

2)

in the resource file,
i changed the occurence of the two static to change their Zorders.
also I have test the WS_EX_TRANSPARENT.

the test cases and the corresponding results are:

a)
LTEXT "bbbbbbbbbbbbbbbbbbbbbbb",IDC_STATIC,42,41,98,20,0,

LTEXT "aaaa",IDC_STATIC,31,15,123,67,0
result:only "aaaa" is visible.
i think this is normal because aaaa is larger than bbbbbbbbbbbb, and aaaa is on top of bbbbbbbbbbb.

b)
LTEXT "bbbbbbbbbbbbbbbbbbbbbbb",IDC_STATIC,42,41,98,20,0,
WS_EX_TRANSPARENT
LTEXT "aaaa",IDC_STATIC,31,15,123,67
result: both are visible;

why??? MSDN states if a window with WS_EX_TRANSPARENT set,windows below it at the position where the original window was initially placed are not obscured and show through.

c)
LTEXT "bbbbbbbbbbbbbbbbbbbbbbb",IDC_STATIC,42,41,98,20,0,
WS_EX_TRANSPARENT
LTEXT "aaaa",IDC_STATIC,31,15,123,67,0,WS_EX_TRANSPARENT
result: same as a) case, only "aaaa" is visible

why????

d)
LTEXT "bbbbbbbbbbbbbbbbbbbbbbb",IDC_STATIC,42,41,98,20
LTEXT "aaaa",IDC_STATIC,31,15,123,67,0,WS_EX_TRANSPARENT
result: same as a) case, only "aaaa" visible
why???

e)

LTEXT "aaaa",IDC_STATIC,31,15,123,67,0,WS_EX_TRANSPARENT
LTEXT "bbbbbbbbbbbbbbbbbbbbbbb",IDC_STATIC,42,41,98,20
result: only "aaaa" is visible
why????

f)
LTEXT "aaaa",IDC_STATIC,31,15,123,67
LTEXT "bbbbbbbbbbbbbbbbbbbbbbb",IDC_STATIC,42,41,98,20
result:both "aaaa" and "bbbbbbbbbbbbbbbbbbbb" is visible.

this case i see why, because bbb is smaller than aaaa, and the Zorder is bbbb above aaaa.

g)
LTEXT "aaaa",IDC_STATIC,31,15,123,67,0,WS_EX_TRANSPARENT
LTEXT "bbbbbbbbbbbbbbbbbbbbbbb",IDC_STATIC,42,41,98,20,0,WS_EX_TRANSPARENT
result: both visible.

h)
LTEXT "aaaa",IDC_STATIC,31,15,123,67
LTEXT "bbbbbbbbbbbbbbbbbbbbbbb",IDC_STATIC,42,41,98,20,0,WS_EX_TRANSPARENT
result: both visible

in e,f,g,h I will think both static should be visible because bbbbb is above "aaaa" no matter whether WS_EX_TRANSPARENT is set with or not. but case e) defeated me.



would you be kind to give me more details? thanks
GeneralRe: why bitmap static or static1 can show up? Pin
Ali Rafiee23-Sep-05 5:46
Ali Rafiee23-Sep-05 5:46 
QuestionTransparent ListBox? Pin
iesoft10-Aug-05 10:39
iesoft10-Aug-05 10:39 
AnswerRe: Transparent ListBox? Pin
Ali Rafiee10-Aug-05 11:17
Ali Rafiee10-Aug-05 11:17 
GeneralRe: Transparent ListBox? Pin
Ali Rafiee11-Aug-05 6:30
Ali Rafiee11-Aug-05 6:30 
GeneralProblem with static method Pin
Ceri29-Jun-05 23:44
Ceri29-Jun-05 23:44 
GeneralRe: Problem with static method Pin
Ali Rafiee1-Jul-05 5:35
Ali Rafiee1-Jul-05 5:35 
AnswerRe: Problem with static method Pin
Douglas R. Keesler6-Mar-06 18:24
Douglas R. Keesler6-Mar-06 18:24 
GeneralDoes not work when control is created via Create() method Pin
Ralph5-Apr-05 9:37
Ralph5-Apr-05 9:37 
GeneralRe: Does not work when control is created via Create() method Pin
Ali Rafiee5-Apr-05 9:51
Ali Rafiee5-Apr-05 9:51 
GeneralRe: Does not work when control is created via Create() method Pin
avatar5d23-Jun-05 15:58
avatar5d23-Jun-05 15:58 
GeneralRe: Does not work when control is created via Create() method Pin
Ali Rafiee21-Sep-05 8:00
Ali Rafiee21-Sep-05 8:00 
General[OFFTOPIC] About first jpg Pin
Klimmtom21-Dec-04 21:03
sussKlimmtom21-Dec-04 21:03 
GeneralRe: [OFFTOPIC] About first jpg Pin
armentage25-Apr-05 5:41
armentage25-Apr-05 5:41 
GeneralShadow Pin
Jaded Hobo21-Dec-04 3:34
Jaded Hobo21-Dec-04 3:34 
GeneralRe: Shadow Pin
Ali Rafiee21-Dec-04 4:13
Ali Rafiee21-Dec-04 4:13 
QuestionDo you have a VC6 version? Pin
WREY15-Dec-04 8:51
WREY15-Dec-04 8:51 
AnswerRe: Do you have a VC6 version? Pin
Ali Rafiee15-Dec-04 9:26
Ali Rafiee15-Dec-04 9:26 

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.