(1)新建一个基于对话框的应用程序
(2) 添加8个编辑框控件
(3)在classview中右击点击New Class.生成一个新类CcolourEdit,基类为CEdit.
在CcolourEdit类中添加一个COLORREF类型的成员变量m_Colour.
private:
COLORREF m_Colour;
(4)进入类向导添加类CcolourEdit的Message中=WM_CTLCOLOR的函数
HBRUSH CcolourEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
// TODO: Change any attributes of the DC here
CDC* dc = GetDC(); //获取画布对象
CRect rect;
GetClientRect(rect); //获取客户区域
rect.InflateRect(1,1,1,1);//将客户区域增大一个像素
CBrush brush (m_Colour);//创建画刷
dc->FrameRect(rect,&brush);//绘制边框
// TODO: Return a non-NULL brush if the parent's handler should not be called
return NULL;
}
(5)添加CcolourEdit的函数(注意在.h文件中要有声明void SetColour(COLORREF Colour)且要为public)
void CcolourEdit::SetColour(COLORREF Colour)
{
m_Colour = Colour;
}
(6)为8个编辑框添加变量分别为m_edit1,m_edit2……,类型为CcolourEdit
(7)在对话框的OnInitDialog()添加代码
BOOL CMy2_056Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
……
m_edit1.SetColour(RGB(255,0,0));
m_edit2.SetColour(RGB(0,0,255));
m_edit3.SetColour(RGB(0,255,0));
m_edit4.SetColour(RGB(255,255,0));
m_edit5.SetColour(RGB(255,0,255));
m_edit6.SetColour(RGB(0,255,255));
m_edit7.SetColour(RGB(128,128,128));
m_edit8.SetColour(RGB(0,0,0));
return TRUE; // return TRUE unless you set the focus to a control
}
(8)注意2_056Dlg.h(对话框头文件)文件中要加入这句代码:#include "ColourEdit.h"