用CFontDialog设置richEdit内容的字体和颜色 及其他

本文介绍如何使用CFontDialog设置RichEdit控件中的字体样式、大小和颜色,并提供了具体实现代码。此外,还介绍了如何获取当前选择文本的字体属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:用CFontDialog设置richEdit内容的字体和颜色 

CHARFORMAT cf;
memset(&cf,0,sizeof(CHARFORMAT));
CFontDialog dlg;
if (IDOK == dlg.DoModal())
{
dlg.GetCharFormat(cf);
m_ctrWordMake.SetDefaultCharFormat(cf);
UpdateData(FALSE);

memset(&cf,0,sizeof(CHARFORMAT));
CFontDialog dlg;
if (IDOK == dlg.DoModal())
{
dlg.GetCharFormat(cf);
m_ctrWordMake.SetDefaultCharFormat(cf);
UpdateData(FALSE);

Invalidate(TRUE);

        }

附加:获取CHARFORMAT cf中相应的值

CHARFORMAT cf;
LOGFONT lf;
memset(&cf, 0, sizeof(CHARFORMAT));
memset(&lf, 0, sizeof(LOGFONT));
//判断是否选择了内容
BOOL bSelect = (GetSelectionType() != SEL_EMPTY) ? TRUE : FALSE;
if (bSelect)
{
            GetSelectionCharFormat(cf);
}
else
{
            GetDefaultCharFormat(cf);
}


//得到相关字体属性
BOOL bIsBold = cf.dwEffects & CFE_BOLD;
BOOL bIsItalic = cf.dwEffects & CFE_ITALIC;
BOOL bIsUnderline = cf.dwEffects & CFE_UNDERLINE;
BOOL bIsStrickout = cf.dwEffects & CFE_STRIKEOUT;


//设置属性
lf.lfCharSet = cf.bCharSet;
lf.lfHeight = cf.yHeight/15;
lf.lfPitchAndFamily = cf.bPitchAndFamily;
lf.lfItalic = bIsItalic;
lf.lfWeight = (bIsBold ? FW_BOLD : FW_NORMAL);
lf.lfUnderline = bIsUnderline;
lf.lfStrikeOut = bIsStrickout;
sprintf(lf.lfFaceName, cf.szFaceName);
            


CFontDialog dlg(&lf);
dlg.m_cf.rgbColors = cf.crTextColor;
if (dlg.DoModal() == IDOK)
{
            dlg.GetCharFormat(cf);//获得所选字体的属性
            if (bSelect) 
                        SetSelectionCharFormat(cf);     //为选定的内容设定所选字体
            else
                        SetWordCharFormat(cf);         //为将要输入的内容设定字体
// SetDefaultCharFormat(cf);
}

二.其他

1.字体大小
LOGFONT lf;   
CFontDialog   dlg(&lf);
if (IDOK == dlg.DoModal())
{
  static CFont font;
  dlg.GetCurrentFont(&lf);
  font.DeleteObject();
  font.CreateFontIndirect(&lf);
  GetDlgItem(IDC_RICHEDIT_WORD)->SetFont(&font);


  COLORREF m_FontColor=dlg.GetColor();
  CDC *pDC=GetDlgItem(IDC_RICHEDIT_WORD)->GetDC();
  pDC->SetTextColor(m_FontColor);
  UpdateData(FALSE);
  GetDlgItem(IDC_RICHEDIT_WORD)->Invalidate();
}
2.字体大小
CFont m_Font;
CRichEditCtrl* pEdt=(CRichEditCtrl*)GetDlgItem(IDC_RICHEDIT_WORD); 
CFont *TempFont=pEdt->GetFont();
// LOGFONT LogFont;
// TempFont->GetLogFont(&LogFont);
CFontDialog cfd;
if(cfd.DoModal()==IDOK)
{
m_Font.Detach();
LOGFONT temp;
cfd.GetCurrentFont(&temp);
m_Font.CreateFontIndirect(&temp);
pEdt->SetFont (&m_Font);
        DEMOINPUTCOLOR = cfd.GetColor();
UpdateData(FALSE);
//        m_ctrWordMake.Invalidate();
// m_ctrWordMake.UpdateWindow();
GetDlgItem(IDC_RICHEDIT_WORD)->Invalidate();
GetDlgItem(IDC_RICHEDIT_WORD)->UpdateWindow();
        }

三.参考网址

http://blog.chinaunix.net/uid-7667983-id-2046556.html


http://blog.sina.com.cn/s/blog_7d1dc9de01011gdh.html


charformat的解析   http://huigezrx.blog.163.com/blog/static/32101652201022055236155/
 修改字体大小及颜色  http://hi.baidu.com/sports98/item/f1e3ee15060b9a731109b56d

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值