打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Yet Another Transparent Static Control

Yet Another Transparent Static Control

By | 15 Dec 2004 | Article
Two other methods of making a transparent static control.
   4.47 (14 votes)
 
39  
Add your own
alternative version

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


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.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
在Visual C++中透明浮动按键的实现
mfc中背景图片随着窗口大小的改变而改变
防止画图时背景的擦除?(传说中的双缓冲)(转载)
VC的内存绘图防止屏幕闪烁技术的实现
内存DC
VC将BMP图片显示在屏幕上(用图片作背景)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服