转自:http://blog.youkuaiyun.com/zgl7903/article/details/5902879
- /
- TransparentDlg.h
- #pragma once
- /
- // CTransparentDlg dialog
- class CTransparentDlg : public CDialog
- {
- protected:
- CBrush m_BkBrush;
- // Construction
- public:
- CTransparentDlg(CWnd* pParent = NULL); // standard constructor
- // Dialog Data
- //{{AFX_DATA(CTransparentDlg)
- enum { IDD = IDD_TRANSPARENTDLG_DIALOG };
- // NOTE: the ClassWizard will add data members here
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CTransparentDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- // Generated message map functions
- //{{AFX_MSG(CTransparentDlg)
- virtual BOOL OnInitDialog();
- afx_msg BOOL OnEraseBkgnd(CDC* pDC);
- afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- // TransparentDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Transparent.h"
- #include "TransparentDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /
- // CTransparentDlg dialog
- CTransparentDlg::CTransparentDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CTransparentDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CTransparentDlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- }
- void CTransparentDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CTransparentDlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CTransparentDlg, CDialog)
- //{{AFX_MSG_MAP(CTransparentDlg)
- ON_WM_ERASEBKGND()
- ON_WM_CTLCOLOR()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /
- // CTransparentDlg message handlers
- BOOL CTransparentDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- // TODO: Add extra initialization here
- //构建背景位图画刷
- CBitmap m_Bitmap;
- if(m_Bitmap.LoadBitmap(IDB_BKBITMAP))
- {
- m_BkBrush.CreatePatternBrush(&m_Bitmap);
- }
- return TRUE; // return TRUE unless you set the focus to a control
- }
- //背景直接使用位图画刷填充
- BOOL CTransparentDlg::OnEraseBkgnd(CDC* pDC)
- {
- // TODO: Add your message handler code here and/or call default
- CRect rcClient;
- GetClientRect(rcClient);
- pDC->FillRect(rcClient, &m_BkBrush);
- return TRUE;
- //return CDialog::OnEraseBkgnd(pDC);
- }
- HBRUSH CTransparentDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
- {
- HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
- // TODO: Change any attributes of the DC here
- //
- {
- CRect rcCtrl;
- pWnd->GetWindowRect( &rcCtrl ); //获取控件位置
- ScreenToClient( &rcCtrl ); //转换到对话框
- //获取ClientDC
- CDC *pBkDC = GetDC();
- //拷贝背景
- pDC->BitBlt(0, 0, rcCtrl.Width(), rcCtrl.Height(),
- pBkDC, rcCtrl.left, rcCtrl.top, SRCCOPY);
- //清理工作
- ReleaseDC( pBkDC ); //释放ClientDC
- //添加其他代码
- //
- pDC->SetBkMode(TRANSPARENT); //背景透明模式
- hbr = (HBRUSH)GetStockObject(NULL_BRUSH); //
- }
- // TODO: Return a different brush if the default is not desired
- return hbr;
- }