Mình đang tìm sơ sơ, thấy có mấy cái hay hay, mình tổng hợp cho dễ hiểu r share lại mn dùng nhé!
If you want to change the background color of a dialog box in MFC, then handle WM_CTLCOLOR/WM_CTLCOLORDLG windows message in your dialog class. You can also use the ON_WM_CTLCOLOR_REFLECT macro to handle the message. Then return the appropriate brush from the message handler code.
If you want to change the background color of your dialog box, it is a very simple.
In your CTestDlg header file, declare a member variable from CBrush:
class CTestDlg : public CDialog
{
…
protected:
CBrush m_brush;
…
};
Then, add this line in the OnInitDialog function:
BEGIN_MESSAGE_MAP(CMFCApplication2Dlg, CDialog)
ON_WM_CTLCOLOR() // send message to change color
END_MESSAGE_MAP()
BOOL CTestDlg::OnInitDialog()
{
…
m_brush.CreateSolidBrush(RGB(255, 255, 255)); // color white brush
…
}
Finally do this on the ID_CTLCOLOR handle:
HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
/*
** No need to do this!
**
** HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
*/
/*
** Return the white brush.
*/
return m_brush;
}
The unsigned integer variable nCtlColor contains one of the following values,
CTLCOLOR_BTN Button control
CTLCOLOR_DLG Dialog box
CTLCOLOR_EDIT Edit control
CTLCOLOR_LISTBOX List-box control
CTLCOLOR_MSGBOX Message box
CTLCOLOR_SCROLLBAR Scroll-bar control
CTLCOLOR_STATIC Static control
Make sure that you delete the brush object when you finish using with it, this will avoid a resource leak.So for my case, I made the m_hBkgndBrush a member variable so I will delete the object in dialog box’s destructor.