VC++ 控件改变编辑框字体和颜色(弹出对话框)
- 作者: 用户43917212
- 来源: 51数据库
- 2021-06-25
调用字体对话框,来改变编辑框的字体和颜色?
1.添加一个编辑框 id为 idc_edit1?
2.为编辑框添加变量 cedit m_edit?
3.添加一个按钮触发该事件
void ctetdlg::onbutton1() //添加一个按钮
{
// todo: add your control notification handler code here
//获得控件的当前字体
logfont lf;
getdlgitem(idc_edit1)->getfont()->getlogfont(&lf);//idc_edit1为编辑框id
//使用按钮的当前字体初始化字体对话框
cfontdialog dlgfontdlg(&lf);
//显示字体选择对话框
if (dlgfontdlg.domodal() == idok)
{
//如果用户在字体选择对话框中单击了“确定”按钮
//则将按钮id_button_demode的标题文本字体设置为所选定的字体
static cfont font;
color = dlgfontdlg.getcolor();//获得选择的颜色 color为colorref类型 在.h文件中声明 (设置颜色)
m_edit.setfocus();//m_edit 为编辑框的控件变量
dlgfontdlg.getcurrentfont(&lf);//获取当前的字体
font.deleteobject(); //删除字体
font.createfontindirect(&lf);//新建
getdlgitem(idc_edit1)->setfont(&font);//设置为新的字体
}
}
4.添加color消息
hbrush ctetdlg::onctlcolor(cdc* pdc, cwnd* pwnd, uint nctlcolor) //添加color消息
{
hbrush hbr = cdialog::onctlcolor(pdc, pwnd, nctlcolor);
// todo: change any attributes of the dc here
if(nctlcolor == ctlcolor_edit)
pdc->settextcolor(color); //初始化颜色
// todo: return a different brush if the default is not desired
return hbr;
}
推荐阅读
