8月14
这个问题MS让很多人伤透了脑筋~偶最初也因为不知道如何改变颜色而浪费了很多时间~后来经过Google+MSDN和自己的摸索,找到了其中的一种方法。(MFC中实现同一种效果往往有很多方法)
首先我们在对话框中添加OnCtlColor消息映射函数,函数作用/函数原型/参数解释请看MSDN描述~
CWnd::OnCtlColor See Also
CWnd Overview | Class Members | Hierarchy Chart | CDC::SetBkColor
The framework calls this member function when a child control is about to be drawn.
afx_msg HBRUSH OnCtlColor(
CDC* pDC,
CWnd* pWnd,
UINT nCtlColor
);
Parameters
pDC
Contains a pointer to the display context for the child window. May be temporary.
pWnd
Contains a pointer to the control asking for the color. May be temporary.
nCtlColor
Contains one of the following values, specifying the type of control:
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
Return Value
OnCtlColor must return a handle to the brush that is to be used for painting the control background.
然后我们在对话框上放置两个STATIC控件,ID分别为:IDC_STCCOLOR和IDC_STCTWO。然后在对话框的类中添加成员变量(别告诉我你不知道怎么添加……),变量需要两个,每个成员变量对应一个控件~
ps:变量种类要选择控件变量~这要的控件变量的类型一般是CStatic~
然后我们在刚才的那个消息映射函数中写下下列代码:
HBRUSH CMFCDialogDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (GetDlgItem(IDC_STCCOLOR) == pWnd)
{
pDC->SetTextColor(RGB(255, 0, 0));
}
else if (GetDlgItem(IDC_STCTWO) == pWnd)
{
pDC->SetTextColor(RGB(0, 0, 255));
}
return hbr;
}
这里之所以要先判断句柄,是因为我们要针对某一个控件进行设置。如果你直接使用pDC->SetTextColor来设置,那么MFC会遍历窗体的所有控件,然后把颜色设置成一样。
至于为什么要添加控件的成员变量,我们可以在对话框的CPP文件中发现这段代码:
void CMFCDialogDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_STCCOLOR, m_cstcColor);
DDX_Control(pDX, IDC_STCTWO, m_cstcTwo);
}
DDE数据交换呵,如果你把两个成员变量的DDE交换给他注释掉,调式后就会发现,颜色无法改变了。
至于为什么,我也不知道。可能是MFC的某种机制吧~其实之前我想过这个是否是因为颜色是状态量,需要一个变量来维持。但是文字内容也是状态量,为什么不需要添加成员变量就能够改变?如果谁知到,麻烦请告诉我~
首先我们在对话框中添加OnCtlColor消息映射函数,函数作用/函数原型/参数解释请看MSDN描述~
引用
CWnd::OnCtlColor See Also
CWnd Overview | Class Members | Hierarchy Chart | CDC::SetBkColor
The framework calls this member function when a child control is about to be drawn.
afx_msg HBRUSH OnCtlColor(
CDC* pDC,
CWnd* pWnd,
UINT nCtlColor
);
Parameters
pDC
Contains a pointer to the display context for the child window. May be temporary.
pWnd
Contains a pointer to the control asking for the color. May be temporary.
nCtlColor
Contains one of the following values, specifying the type of control:
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
Return Value
OnCtlColor must return a handle to the brush that is to be used for painting the control background.
然后我们在对话框上放置两个STATIC控件,ID分别为:IDC_STCCOLOR和IDC_STCTWO。然后在对话框的类中添加成员变量(别告诉我你不知道怎么添加……),变量需要两个,每个成员变量对应一个控件~
ps:变量种类要选择控件变量~这要的控件变量的类型一般是CStatic~
然后我们在刚才的那个消息映射函数中写下下列代码:
引用
HBRUSH CMFCDialogDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (GetDlgItem(IDC_STCCOLOR) == pWnd)
{
pDC->SetTextColor(RGB(255, 0, 0));
}
else if (GetDlgItem(IDC_STCTWO) == pWnd)
{
pDC->SetTextColor(RGB(0, 0, 255));
}
return hbr;
}
这里之所以要先判断句柄,是因为我们要针对某一个控件进行设置。如果你直接使用pDC->SetTextColor来设置,那么MFC会遍历窗体的所有控件,然后把颜色设置成一样。
至于为什么要添加控件的成员变量,我们可以在对话框的CPP文件中发现这段代码:
引用
void CMFCDialogDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_STCCOLOR, m_cstcColor);
DDX_Control(pDX, IDC_STCTWO, m_cstcTwo);
}
DDE数据交换呵,如果你把两个成员变量的DDE交换给他注释掉,调式后就会发现,颜色无法改变了。
至于为什么,我也不知道。可能是MFC的某种机制吧~其实之前我想过这个是否是因为颜色是状态量,需要一个变量来维持。但是文字内容也是状态量,为什么不需要添加成员变量就能够改变?如果谁知到,麻烦请告诉我~
出处:KingsamChen
转载时必须以链接形式注明出处及本声明!
修改QQAllInOne去聊天窗口左下角广告信息
视频: 《国足欢迎你》群星版


