1.之前在网上看到乱七八糟写给对话框添加背景图片的,然后实现对话框上的子控件透明的例子,很多都行不通,特意把具体实现写到博客上,给有需要的新手或者老手参考下。
VideoSetDlg.h
class CVideoSetDlg : public CDialogEx
{
public:
HBRUSH OnCtlColor(CDC* pDC,CWnd* pWnd,UINT nCtlColor);
protected:
//复写初始化对话框函数
virtual BOOL OnInitDialog();
private:
CImage m_imgBackGround;//背景图片,图片格式为png格式
CBrush m_bkBrush;
};
VideoSetDlg.cpp
BEGIN_MESSAGE_MAP(CVideoSetDlg,CDialogEx)
ON_WM_CTLCOLOR()//需要加上,消息映射
END_MESSAGE_MAP
BOOL CVideoSetDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
CString strPngPath("background.png");
m_imgBackGround.Load(strPngPath);
CBitmap bmpTmp;
bmpTmp.Attach(m_imgBackGround.Detach());
m_bkBrush.CreatePatternBrush(&bmpTmp);
//美化对画框外观【圆角对话框】,可不要。
CRgn rgnTmp;
RECT rc;
GetClientRect(&rc);
rgnTmp.CreateRoundRectRgn(rc.left + 5,rc.top + 5,rc.right - rc.left,rc.bottom - rc.top,5,5);
SetWindowRgn(rgnTmp,TRUE);
return TRUE;
}
HBRUSH CVideoSetDlg::OnCtlColor(CDC* pDC,CWnd* pWnd,UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC,pWnd,nCtlColor);
UINT id = pWnd->GetDlgCtrlID();
if (id == IDC_XXX ){
pDC->SetBkMode(TRANSPARENT);
CRect rc;
pWnd->GetWindowRect(&rc);
ScreenToClient(&rc);
CDC* dc = GetDC();
pDC->BitBlt(0,0,rc.Width(),rc.Height(),dc,rc.left,rc.top,SRCCOPY); //把父窗口背景先画到子控件上去。
ReleaseDC(dc);
return (HBRUSH)::GetStockObject(NULL_BRUSH);
}
if (this == pWnd){
return m_bkBrush;
}
}
2.OnEraseBkgnd格式
//声明
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
//在消息映射中添加
ON_WM_ERASEBKGND()
//实现
BOOL CTest::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}