1、使用eVC创建一个基于MFC的应用程序SpinUse;
2、设计界面如图:
对话框空间属性如下
标识(ID) | 描述 |
IDC_EDT_RED | 编辑框,范围为0-255,对应变量为m_red |
IDC_EDT_GREEN | 编辑框,范围为0-255,对应变量为m_green |
IDC_EDT_BLUE | 编辑框,范围为0-255,对应变量为m_blue |
IDC_SPIN_RED | 微调按钮,需要选中SER BUDDY INTEGER复选框 |
IDC_SPIN_GREEN | 微调按钮,需要选中SER BUDDY INTEGER复选框 |
IDC_SPIN_BLUE | 微调按钮,需要选中SER BUDDY INTEGER复选框 |
3、在CSPinUseDlg的OnInitDialog中添加如下代码
CSpinButtonCtrl * pSpinRed= (CSpinButtonCtrl*)GetDlgItem(IDC_SPIN_RED);
ASSERT(pSpinRed!=NULL);
pSpinRed->SetBuddy(GetDlgItem(IDC_EDT_RED));
pSpinRed->SetRange(0,255);
pSpinRed->SetPos(128);
CSpinButtonCtrl * pSpinGreen= (CSpinButtonCtrl*)GetDlgItem(IDC_SPIN_GREEN);
ASSERT(pSpinGreen!=NULL);
pSpinGreen->SetBuddy(GetDlgItem(IDC_EDT_GREEN));
pSpinGreen->SetRange(0,255);
pSpinGreen->SetPos(128);
CSpinButtonCtrl * pSpinBlue= (CSpinButtonCtrl*)GetDlgItem(IDC_SPIN_BLUE);
ASSERT(pSpinBlue!=NULL);
pSpinBlue->SetBuddy(GetDlgItem(IDC_EDT_BLUE));
pSpinBlue->SetRange(0,255);
pSpinBlue->SetPos(128);
ASSERT(pSpinRed!=NULL);
pSpinRed->SetBuddy(GetDlgItem(IDC_EDT_RED));
pSpinRed->SetRange(0,255);
pSpinRed->SetPos(128);
CSpinButtonCtrl * pSpinGreen= (CSpinButtonCtrl*)GetDlgItem(IDC_SPIN_GREEN);
ASSERT(pSpinGreen!=NULL);
pSpinGreen->SetBuddy(GetDlgItem(IDC_EDT_GREEN));
pSpinGreen->SetRange(0,255);
pSpinGreen->SetPos(128);
CSpinButtonCtrl * pSpinBlue= (CSpinButtonCtrl*)GetDlgItem(IDC_SPIN_BLUE);
ASSERT(pSpinBlue!=NULL);
pSpinBlue->SetBuddy(GetDlgItem(IDC_EDT_BLUE));
pSpinBlue->SetRange(0,255);
pSpinBlue->SetPos(128);
4、用类向导添加在个颜色编辑框的OnChange函数。并添加如下代码。
void CSpinUseDlg::OnColorChange()
{
UpdateData(TRUE);
CBrush colorBrush;
COLORREF clRGB;
clRGB=RGB(m_red,m_green,m_blue);
CClientDC*pClientDC;
pClientDC= new CClientDC(this);
colorBrush.CreateSolidBrush(clRGB);
CRect rect(100,200,200,300);
pClientDC->FillRect(rect,&colorBrush);
delete pClientDC;
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
5、完成编译
{
UpdateData(TRUE);
CBrush colorBrush;
COLORREF clRGB;
clRGB=RGB(m_red,m_green,m_blue);
CClientDC*pClientDC;
pClientDC= new CClientDC(this);
colorBrush.CreateSolidBrush(clRGB);
CRect rect(100,200,200,300);
pClientDC->FillRect(rect,&colorBrush);
delete pClientDC;
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}