CEdit编辑框字体和背景设置

时间:2023-10-17 12:23:32

CEdit编辑框字体和背景设置
注意事项:当CEdit为“disable”时,设置编辑框的字体和背景会没有效果。
解决方案:将CEdit的Style设置为“readonly”,这样设置就能生效了,同时也能达到禁用编辑的功能。

通过发送“WM_CTLCOLOR”消息,调用HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)来对编辑框字体和背景进行设置。
参数nCtlColor用于指定控件的类型,可以是:
.CTLCOLOR_BTN 按钮控件 //(注意check box 属于button控件)
.CTLCOLOR_DLG 对话框
.CTLCOLOR_EDIT 编辑框
.CTLCOLOR_LISTBOX 列表控件
.CTLCOLOR_MSGBOX 消息控件
.CTLCOLOR_SCROLLBAR 滚动条控件
.CTLCOLOR_STATIC 静态控件
[程序实现]
假设你已有了名为My的对话框工程.你有了一个STATIC的控件,ID为IDC_STATIC1.
HBRUSH CMyDlg::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_STATIC)
{
pDC->SetTextColor(RGB(255,0,0)); //字体颜色
pDC->SetBkColor(RGB(0, 0, 255)); //字体背景色
}
// TODO: Return a different brush if the default is not desired
return hbr;
}

如果要指定某个特定控件可以这样写:ID为IDC_STATIC1
if (pWnd->GetDlgCtrlID()==IDC_STATIC1)
{
pDC->SetTextColor(RGB(255,0,0)); //设置字体颜色
pDC->SetBkMode(TRANSPARENT); //设置字体背景为透明
// TODO: Return a different brush if the default is not desired
return (HBRUSH)::GetStockObject(BLACK_BRUSH); // 设置背景色
}
【注】
BLACK_BRUSH:黑色
WHITE_BRUSH:白色
GRAY_BRUSH:灰色
NULL_BRUSH:透明
HOLLOW_BRUSH :透明