调用字体对话框,来改变编辑框的字体和颜色
1.添加一个编辑框 ID为 IDC_EDIT1
2.为编辑框添加变量 CEdit m_edit
3.添加一个按钮触发该事件
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;
}

本文介绍如何通过按钮触发事件来调用字体对话框,实现编辑框内的字体样式及颜色的更改。具体步骤包括:创建编辑框并添加控件变量,定义按钮点击事件处理函数,获取编辑框当前字体,展示字体选择对话框,并根据用户选择更新字体和颜色。
1万+

被折叠的 条评论
为什么被折叠?



