首先了解一下CHARFORMAT结构
SetDefaultCharFormat( CHARFORMAT& cf );
typedef struct _charformat {
UINT cbSize;
_WPAD _wPad1;
DWORD dwMask;
DWORD dwEffects;
LONG yHeight;
LONG yOffset;
COLORREF crTextColor;
BYTE bCharSet;
BYTE bPitchAndFamily;
TCHAR szFaceName[LF_FACESIZE];
_WPAD _wPad2;
} CHARFORMAT;
其中bCharSet有如下值
lfCharSet
Specifies the character set. The following values are predefined:
ANSI_CHARSET
BALTIC_CHARSET
CHINESEBIG5_CHARSET
DEFAULT_CHARSET
EASTEUROPE_CHARSET
GB2312_CHARSET
GREEK_CHARSET
HANGUL_CHARSET
MAC_CHARSET
OEM_CHARSET
RUSSIAN_CHARSET
SHIFTJIS_CHARSET
SYMBOL_CHARSET
TURKISH_CHARSET
Korean Windows:
JOHAB_CHARSET
Middle-Eastern Windows:
HEBREW_CHARSET
ARABIC_CHARSET
Thai Windows:
THAI_CHARSET
OnChangeFont() 是对话框中一按钮消息响应函数
void CTransformDlg::OnChangeFont()
{
// TODO: Add your control notification handler code here
CHARFORMAT cf;
LOGFONT lf;
memset(&cf, 0, sizeof(CHARFORMAT));
memset(&lf, 0, sizeof(LOGFONT));
//判断是否选择了内容
BOOL m_bSelect = (m_RichEditCtrlTS.GetSelectionType() != SEL_EMPTY) ? TRUE : FALSE;
if (m_bSelect) {
m_RichEditCtrlTS.GetSelectionCharFormat(cf);
} else {
m_RichEditCtrlTS.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);
//strcpy(lf.lfFaceName, cf.szFaceName);
CFontDialog dlg(&lf);
dlg.m_cf.rgbColors = cf.crTextColor;
if (dlg.DoModal() == IDOK) {
dlg.GetCharFormat(cf); //获得所选的字体属性,如字体、颜色、大小等
if (m_bSelect)
m_RichEditCtrlTS.SetSelectionCharFormat(cf); //为选定的内容设定所选字体
else
m_RichEditCtrlTS.SetWordCharFormat(cf); //为将要输入的内容设定字体
//m_RichEditCtrlTS.SetDefaultCharFormat(cf); //设置输入框内所有字符的字体,包括已经输入的和将要输入的字符
}
}
CFont 说明
CFont font;
VERIFY(font.CreateFont(
12, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_NORMAL, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
"Arial")); // lpszFacename