Method One: Call the member function of the CWinApp class SetDialogBkColor to implement. (This function is deprecated)
1 |
void SetDialogBkColor( COLORREF clrCtlBk = RGB(192, 192, 192), COLORREF clrCtlText = RGB(0, 0, 0)); |
Where the first parameter of the function specifies the background color, the second parameter specifies the text color.
Add the following code in InitInstance ():
1 |
SetDialogBkColor(RGB(0,0,255),RGB(255,0,0)); |
It is important to note that SetDialogBkColor must be called before calling DoModal ().
This method has been verified by the author and cannot change the background of the dialog box
Method Two: Overload OnPaint (), which is the WM_PAINT message
1234 |
CRect rect; CPaintDC dc( this ); GetClientRect(rect); dc.FillSolidRect(rect,RGB(0,255,0)); //设置为绿色背景 |
Method Three: Overload OnCtlColor (cdc* PDC, cwnd* pWnd, UINT nCtlColor), which is the WM_CTLCOLOR message
. h header File Additions CBrush M_brush;
. cpp source file Additions M_brush. CreateSolidBrush (RGB (255,0,0));
1234567 |
onctlcolor (cdc* pdc, cwnd* pwnd, Code class= "CPP Color1 bold" >uint nctlcolor) Code class= "CPP Plain" >{ /* downlink code to comment out       hbrush hbr = Cdialog::onctlcolor (pDC, pwnd, nCtlColor); */ return m_brush; Code class= "CPP Comments" >//back add red brush } |
Method Four: or overload OnCtlColor (cdc* PDC, cwnd* pWnd, UINT nCtlColor), which is the WM_CTLCOLOR message
. h header File Additions CBrush M_brush;
. cpp source file Additions M_brush. CreateSolidBrush (RGB (255,0,0));
12345678 |
OnCtlColor(CDC* pDC, CWnd* pWnd,
UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
//在这加一条是否为对话框的判断语句
if
(nCtlColor ==CTLCOLOR_DLG)
return
m_brush;
//返加红色刷子
return
hbr;
}
|
Different paint brushes can be returned depending on the type of control to achieve different color settings for controls
1234567 |
CTLCOLOR_BTN 按钮控件
CTLCOLOR_DLG 对话框
CTLCOLOR_EDIT 编辑框
CTLCOLOR_LISTBOX 列表控件
CTLCOLOR_MSGBOX 消息控件
CTLCOLOR_SCROLLBAR 滚动条控件
CTLCOLOR_STATIC 静态控件
|
1 |
这里大家要注意,OnCtlColor能改变Static等子控件的颜色,对于Button必须设置其属性Owner Draw为True,才能改变Button按钮背景色(CButton 文本的字体颜色并不能通过SetBkColor来改变,需要自己重绘CButton,在DrawItem中进行实现。 |
123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
HBRUSH
CXXXDlg::OnCtlColor(CDC* pDC, CWnd* pWnd,
UINT
nCtlColor)
{
HBRUSH
hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: 在此更改 DC 的任何属性
if
(nCtlColor==CTLCOLOR_BTN)
//更改按钮颜色
{
//pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkColor(RGB(121,121,255));
HBRUSH
b=CreateSolidBrush(RGB(121,121,255));
return
b;
}
else
if
(nCtlColor==CTLCOLOR_SCROLLBAR)
//
{
//pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkColor(RGB(233,233,220));
HBRUSH
b=CreateSolidBrush(RGB(233,233,220));
return
b;
}
else
if
(nCtlColor==CTLCOLOR_EDIT)
//更改编辑框
{
//pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkColor(RGB(165,254,236));
HBRUSH
b=CreateSolidBrush(RGB(165,254,236));
return
b;
}
else
if
(nCtlColor==CTLCOLOR_STATIC)
//更改静态文本
{
pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkColor(RGB(166,254,1));
HBRUSH
b=CreateSolidBrush(RGB(166,254,1));
return
b;
}
else
if
(nCtlColor==CTLCOLOR_DLG)
//更改对话框背景色
{
pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkColor(RGB(166,254,1));
HBRUSH
b=CreateSolidBrush(RGB(166,254,1));
return
b;
}
// TODO: 如果默认的不是所需画笔,则返回另一个画笔
return
hbr;
}
|
Vc/mfc How to set the background color of a dialog box