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

 

/
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;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值