要改变对话框的一般颜色,可以在C
***
App的InitInstance函数里加入如下代码:
SetDialogBkColor(RGB( 0 , 255 , 255 ),RGB( 255 , 0 , 0 )); // 背景青蓝、文字红色
SetDialogBkColor(RGB( 0 , 255 , 255 ),RGB( 255 , 0 , 0 )); // 背景青蓝、文字红色
每个控件在dialog中都是一个窗口,当要绘制控件时,会发出一个WM_CTLCOLOR消息给它的父窗口(对话框本身).消息映射及响应函数如下:
ON_WM_CRLCOLOR() //反映WM_CTLCOLOR消息
afx_msg HBRUSH CWnd::OnCrlColor(CDC* pDC,CWnd* pWnd,UINT nCtlColor);
//
nCtlColor类型:
CTLCOLOR_DLG对话框本身,不含有所有的控件
CTLCOLOR_STATIC 所有包含static text控件的设置(也包括无效的edit box、combo box的eidt box)
CTLCOLOR_EDIT edit box与combo box的eidt box部分
CTLCOLOR_LIST list box与combo box的edit box部分
CTLCOLOR_SCROLLBAR Scroll bar的空白区
我们也可以单独改变某一个控件的颜色.OnCtrlColor函数的pWnd成员变量是各控件的窗口指针,以pWnd->GetDlgCtrlID()可以获得调用此函数的控件ID,将它与已知ID比较,就可以改变这个控件的颜色。另外一个作法是比较pWnd;例如:如果一个edit box控件的成员变量m_edit1被声明为CEdit类,那么把m_edit1.m_hWnd与pWnd->m_hWnd比较,也可以知道是哪个控件.
下面是一个例子:
HBRUSH C
***
Dlg::OnCtlColor(CDC
*
pDC, CWnd
*
pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if (nCtlColor == CTLCOLOR_LISTBOX)
{
HBRUSH m_hbrush;
m_hbrush = CreateSolidBrush(RGB( 0 , 0 , 0 ));
pDC -> SetBkMode(TRANSPARENT);
pDC -> SetTextColor(RGB( 255 , 255 , 255 ));
return m_hbrush;
}
CBrush m_cBrush;
m_cBrush.CreateSolidBrush(RGB( 0 , 255 , 255 ));
switch (nCtlColor)
{
case CTLCOLOR_DLG:
return m_cBrush;
break ;
case CTLCOLOR_STATIC:
pDC -> SetTextColor(RGB( 255 , 0 , 0 ));
pDC -> SetBkColor(RGB( 0 , 255 , 255 ));
return (HBRUSH)m_cBrush;
break ;
}
DeleteObject(m_cBrush);
// TODO: Return a different brush if the default is not desired
return hbr;
}
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if (nCtlColor == CTLCOLOR_LISTBOX)
{
HBRUSH m_hbrush;
m_hbrush = CreateSolidBrush(RGB( 0 , 0 , 0 ));
pDC -> SetBkMode(TRANSPARENT);
pDC -> SetTextColor(RGB( 255 , 255 , 255 ));
return m_hbrush;
}
CBrush m_cBrush;
m_cBrush.CreateSolidBrush(RGB( 0 , 255 , 255 ));
switch (nCtlColor)
{
case CTLCOLOR_DLG:
return m_cBrush;
break ;
case CTLCOLOR_STATIC:
pDC -> SetTextColor(RGB( 255 , 0 , 0 ));
pDC -> SetBkColor(RGB( 0 , 255 , 255 ));
return (HBRUSH)m_cBrush;
break ;
}
DeleteObject(m_cBrush);
// TODO: Return a different brush if the default is not desired
return hbr;
}