vs2008 动态设置Edit Control控件的背景及字体颜色

1.新建一个基于对话框的应用程序,在对话框上添加一个Edit控件和三个Radio控件
2.修改Radio控件的ID分别为:IDC_RAD_RED、IDC_RAD_GREED、IDC_RAD_BLUE,名称对应修改为red、green、blue
3.向对话框对应的类中添加如下成员变量:
public:
COLORREF m_colorEditText; // edit控件的字体颜色
COLORREF m_colorEditBK; // edit控件的背景颜色
CBrush* m_pEditBkBrush;
并在构造函数中初始化:
CColorEditDlgDlg::CColorEditDlgDlg(CWnd* pParent )
: CDialog(CColorEditDlgDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CColorEditDlgDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

m_colorEditText = RGB(255,255,255);
m_colorEditBK = RGB(0,0,0);
m_pEditBkBrush = new CBrush(RGB(0,0,0));
}
4.向对话框添加WM_CTLCOLOR和WM_DESTORY消息的响应函数如下:
HBRUSH CColorEditDlgDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
switch (nCtlColor) {

case CTLCOLOR_EDIT:
case CTLCOLOR_MSGBOX:
// Set color to green on black and return the background brush.
pDC->SetTextColor(m_colorEditText);
pDC->SetBkColor(m_colorEditBK);
return (HBRUSH)(m_pEditBkBrush->GetSafeHandle());

default:
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}


}

void CColorEditDlgDlg::OnDestroy()
{
CDialog::OnDestroy();

// TODO: Add your message handler code here
delete m_pEditBkBrush;

}
5.分别向三个Radio控件添加BN_CLICKED消息响应函数如下:
void CColorEditDlgDlg::OnRadRed()
{
// TODO: Add your control notification handler code here

m_colorEditText = RGB(255,0,0);

// redraw edit control
((CEdit *)GetDlgItem(IDC_EDIT1))->SetRedraw(TRUE);

// invalidate the entire control, force painting
((CEdit *)GetDlgItem(IDC_EDIT1))->Invalidate();
((CEdit *)GetDlgItem(IDC_EDIT1))->UpdateWindow();

}

void CColorEditDlgDlg::OnRadBlue()
{
// TODO: Add your control notification handler code here
m_colorEditText = RGB(0,0,255);

// redraw edit control
((CEdit *)GetDlgItem(IDC_EDIT1))->SetRedraw(TRUE);

// invalidate the entire control, force painting
((CEdit *)GetDlgItem(IDC_EDIT1))->Invalidate();
((CEdit *)GetDlgItem(IDC_EDIT1))->UpdateWindow();

}

void CColorEditDlgDlg::OnRadGreen()
{
// TODO: Add your control notification handler code here
m_colorEditText = RGB(0,255,0);

// redraw edit control
((CEdit *)GetDlgItem(IDC_EDIT1))->SetRedraw(TRUE);

// invalidate the entire control, force painting
((CEdit *)GetDlgItem(IDC_EDIT1))->Invalidate();
((CEdit *)GetDlgItem(IDC_EDIT1))->UpdateWindow();

}

完成后运行程序,点击Radio控件可以动态改变编辑框中字体的颜色,如果想改变edit框背景的颜色,可以修改成员变量m_colorEditBK的值并重绘控件。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值