实现对话框在位图作为背景时的控件透明

转自:http://blog.csdn.net/zgl7903/article/details/5902879

 

[cpp]  view plain copy
  1. /  
  2. TransparentDlg.h   
  3.   
  4. #pragma once  
  5. /  
  6. // CTransparentDlg dialog  
  7.   
  8. class CTransparentDlg : public CDialog  
  9. {  
  10. protected:  
  11.   CBrush m_BkBrush;  
  12.   
  13. // Construction  
  14. public:  
  15.   CTransparentDlg(CWnd* pParent = NULL);    // standard constructor  
  16.   
  17. // Dialog Data  
  18.   //{{AFX_DATA(CTransparentDlg)  
  19.   enum { IDD = IDD_TRANSPARENTDLG_DIALOG };  
  20.   // NOTE: the ClassWizard will add data members here  
  21.   //}}AFX_DATA  
  22.   
  23. // ClassWizard generated virtual function overrides  
  24.   //{{AFX_VIRTUAL(CTransparentDlg)  
  25. protected:  
  26.   virtual void DoDataExchange(CDataExchange* pDX);  // DDX/DDV support  
  27.   //}}AFX_VIRTUAL  
  28.   
  29. // Implementation  
  30. protected:  
  31.    
  32. // Generated message map functions  
  33.   //{{AFX_MSG(CTransparentDlg)  
  34.   virtual BOOL OnInitDialog();  
  35.   afx_msg BOOL OnEraseBkgnd(CDC* pDC);  
  36.   afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);  
  37.   //}}AFX_MSG  
  38.   DECLARE_MESSAGE_MAP()  
  39.   
  40. };  

 

 

[cpp]  view plain copy
  1. // TransparentDlg.cpp : implementation file  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "Transparent.h"  
  6. #include "TransparentDlg.h"  
  7.   
  8. #ifdef _DEBUG  
  9. #define new DEBUG_NEW  
  10. #undef THIS_FILE  
  11. static char THIS_FILE[] = __FILE__;  
  12. #endif  
  13.   
  14. /  
  15. // CTransparentDlg dialog  
  16.   
  17. CTransparentDlg::CTransparentDlg(CWnd* pParent /*=NULL*/)  
  18.     : CDialog(CTransparentDlg::IDD, pParent)  
  19. {  
  20.     //{{AFX_DATA_INIT(CTransparentDlg)  
  21.         // NOTE: the ClassWizard will add member initialization here  
  22.     //}}AFX_DATA_INIT  
  23.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32  
  24. }  
  25.   
  26. void CTransparentDlg::DoDataExchange(CDataExchange* pDX)  
  27. {  
  28.     CDialog::DoDataExchange(pDX);  
  29.     //{{AFX_DATA_MAP(CTransparentDlg)  
  30.         // NOTE: the ClassWizard will add DDX and DDV calls here  
  31.     //}}AFX_DATA_MAP  
  32. }  
  33.   
  34. BEGIN_MESSAGE_MAP(CTransparentDlg, CDialog)  
  35.     //{{AFX_MSG_MAP(CTransparentDlg)  
  36.     ON_WM_ERASEBKGND()  
  37.     ON_WM_CTLCOLOR()  
  38.     //}}AFX_MSG_MAP  
  39. END_MESSAGE_MAP()  
  40.   
  41. /  
  42. // CTransparentDlg message handlers  
  43.   
  44. BOOL CTransparentDlg::OnInitDialog()  
  45. {  
  46.    CDialog::OnInitDialog();  
  47.   
  48.   // Set the icon for this dialog.  The framework does this automatically  
  49.   //  when the application's main window is not a dialog  
  50.   // TODO: Add extra initialization here  
  51.   
  52.   //构建背景位图画刷  
  53.   CBitmap m_Bitmap;  
  54.   if(m_Bitmap.LoadBitmap(IDB_BKBITMAP))  
  55.   {  
  56.     m_BkBrush.CreatePatternBrush(&m_Bitmap);  
  57.   }  
  58.       
  59.   return TRUE;  // return TRUE  unless you set the focus to a control  
  60. }  
  61.   
  62. //背景直接使用位图画刷填充  
  63. BOOL CTransparentDlg::OnEraseBkgnd(CDC* pDC)   
  64. {  
  65.   // TODO: Add your message handler code here and/or call default  
  66.   CRect rcClient;  
  67.   GetClientRect(rcClient);  
  68.   pDC->FillRect(rcClient, &m_BkBrush);  
  69.   return TRUE;  
  70.   
  71.   //return CDialog::OnEraseBkgnd(pDC);  
  72. }  
  73.   
  74. HBRUSH CTransparentDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)   
  75. {  
  76.   HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);  
  77.       
  78.   // TODO: Change any attributes of the DC here  
  79.   //  
  80.   {      
  81.     CRect rcCtrl;     
  82.     pWnd->GetWindowRect( &rcCtrl ); //获取控件位置     
  83.     ScreenToClient( &rcCtrl ); //转换到对话框     
  84.       
  85.     //获取ClientDC     
  86.     CDC *pBkDC = GetDC();      
  87.       
  88.     //拷贝背景     
  89.     pDC->BitBlt(0, 0, rcCtrl.Width(), rcCtrl.Height(),     
  90.       pBkDC, rcCtrl.left, rcCtrl.top, SRCCOPY);      
  91.       
  92.     //清理工作     
  93.     ReleaseDC( pBkDC ); //释放ClientDC     
  94.       
  95.     //添加其他代码     
  96.     //     
  97.       
  98.     pDC->SetBkMode(TRANSPARENT); //背景透明模式     
  99.     hbr = (HBRUSH)GetStockObject(NULL_BRUSH); //     
  100.   }   
  101.     
  102.   // TODO: Return a different brush if the default is not desired  
  103.   return hbr;  
  104. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值