您的位置:首页 > 其它

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

2010-09-24 10:00 417 查看


// 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: