天天看點

MFC添加背景圖檔,位圖

标題一:添加背景圖檔使用CBrush 背景刷色

TestDlg.h中 
CBrush m_brBk;//在public中定義 

TestDlg.cpp中 
在初始化函數OnInitDialog()中加入: 
BOOL CTestDlg::OnInitDialog() 
{ 
    CDialog::OnInitDialog(); 
    CBitmap bmp; 
    bmp.LoadBitmap(IDB_BITMAP2); 
    m_brBk.CreatePatternBrush(&bmp); 
    bmp.DeleteObject(); 
    return TRUE; // return TRUE unless you set the focus to a control 
} 

在打開類向導,找到WM_CTLCOLOR消息,重載得對應函數OnCtlColor(),添加如下: 
HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{ 
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); 

    if (pWnd == this) 
    { 
        return m_brBk; 
    } 
    return hbr; 
}
           

标題二:添加button位圖

1.MyButtonDlg.h 裡添加

CBitmapButton m_close;
           

2.關聯m_close

MFC添加背景圖檔,位圖

3.資源視圖裡添加bitmap位圖

MFC添加背景圖檔,位圖

4.OnInitDialog()函數添加以下代碼

BOOL CMyButtonDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    CBitmap bmp;
    bmp.LoadBitmap(IDB_BITMAP1);
    m_brbk.CreatePatternBrush(&bmp);
    bmp.DeleteObject();
    //OnInitDialog()函數添加以下代碼
    **m_close.LoadBitmaps(IDB_BITMAP3,IDB_BITMAP1,IDB_         BITMAP3,IDB_BITMAP2);
    m_close.SizeToContent();//按鈕适用圖檔大小**
    ...
           

5.OVER效果圖

MFC添加背景圖檔,位圖