获取对话框当前cfont_字体对话框 CFontDialog | 学步园

设置RichEdit控件中的字体

第1步: 定义CHARFORMAT 变量

CHARFORMAT charFormat;

第2步:获取CRichEditCtrl控件中的默认字体格式

CHARFORMAT charFormat;

m_richEdit.GetDefaultCharFormat(charFormat);

第3步: 用控件默认字体初始化字体对话框

CFontDialog fontDlg(charFormat);

第4步: 使用字体对话框设置控件中的字体

if (fontDlg.DoModal()==IDOK)

{

fontDlg.GetCharFormat(charFormat);

m_richEdit.SetSelectionCharFormat(charFormat);

//m_richEdit.SetDefaultCharFormat(charFormat);

}

全部代码如下:

CHARFORMAT charFormat;

m_richEdit.GetDefaultCharFormat(charFormat);

CFontDialog fontDlg(charFormat);

if (fontDlg.DoModal()==IDOK)

{

fontDlg.GetCharFormat(charFormat);

m_richEdit.SetSelectionCharFormat(charFormat);

//m_richEdit.SetDefaultCharFormat(charFormat);

}

注:

不要定义为CFONT  类型

因为  m_RichEdit.GetFont()  得不到控件中字体的信息,   可能是因为GetFont() 是继承父类

而     m_RichEdit.GetCharFormat() 可以得到控件中字体信息,因为GetCharFormat()函数为RichEdit控件类本身定义的函数,不是继承父类方法

参考资料:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于C++基于MFC设置对话框、所有控件和字体自适应放缩程序,可以采用以下步骤: 1. 在对话框类的头文件中添加如下成员变量: ```cpp CRect m_rectDlg; // 对话框矩形区域 CFont m_font; // 字体 ``` 2. 在OnInitDialog函数中初始化成员变量: ```cpp BOOL CMyDialog::OnInitDialog() { CDialogEx::OnInitDialog(); // 获取对话框矩形区域 GetClientRect(m_rectDlg); // 获取字体 LOGFONT lf; GetFont()->GetLogFont(&lf); m_font.CreateFontIndirect(&lf); // 设置所有控件自适应放缩 ModifyStyleEx(0, WS_EX_CONTROLPARENT); return TRUE; } ``` 3. 在OnSize函数中实现对所有控件的自适应放缩: ```cpp void CMyDialog::OnSize(UINT nType, int cx, int cy) { CDialogEx::OnSize(nType, cx, cy); // 获取当前对话框矩形区域 CRect rect; GetClientRect(rect); // 计算放缩比例 float fRateX = (float)rect.Width() / (float)m_rectDlg.Width(); float fRateY = (float)rect.Height() / (float)m_rectDlg.Height(); // 放缩字体 LOGFONT lf; m_font.GetLogFont(&lf); lf.lfHeight = (LONG)(lf.lfHeight * fRateY); m_font.Detach(); m_font.CreateFontIndirect(&lf); SetFont(&m_font); // 放缩控件 CWnd* pChild = GetWindow(GW_CHILD); while (pChild) { CRect rectChild; pChild->GetWindowRect(rectChild); ScreenToClient(rectChild); rectChild.left = (int)(rectChild.left * fRateX); rectChild.right = (int)(rectChild.right * fRateX); rectChild.top = (int)(rectChild.top * fRateY); rectChild.bottom = (int)(rectChild.bottom * fRateY); pChild->MoveWindow(rectChild); pChild = pChild->GetNextWindow(); } // 更新对话框矩形区域 m_rectDlg = rect; } ``` 通过以上步骤,即可实现C++基于MFC设置对话框、所有控件和字体自适应放缩程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值