一:用CFontDialog设置richEdit内容的字体和颜色
CHARFORMAT cf;
memset(&cf,0,sizeof(CHARFORMAT));
CFontDialog dlg;
if (IDOK == dlg.DoModal())
{
dlg.GetCharFormat(cf);
m_ctrWordMake.SetDefaultCharFormat(cf);
UpdateData(FALSE);
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