调用字体对话框,来改变编辑框的字体和颜色
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);//设置为新的字体
- }
- }
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;
- }