VC 如何获取一个控件内的字体
我在对话框上放了一个RichText控件,在打开字体对话框的时候,能自动获取该控件的字体,并显示!我该如何做呢?...
我在对话框上放了一个RichText控件,在打开字体对话框的时候,能自动获取该控件的字体,并显示!我该如何做呢?
展开
3个回答
展开全部
首先了解一下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
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
展开全部
//获得控件的当前字体
LOGFONT lf;
GetDlgItem(IDC_EDIT1)->GetFont()->GetLogFont(&lf);
//使用按钮的当前字体初始化字体对话框
CFontDialog dlgFontDlg(&lf);
//显示字体选择对话框
if (dlgFontDlg.DoModal() == IDOK)
{
//如果用户在字体选择对话框中单击了“确定”按钮
//则将按钮IDC_EDIT1的标题文本字体设置为所选定的字体
static CFont font;
COLORREF a;
CWnd* pWnd=GetDlgItem(IDC_EDIT1);
CDC* pDC=GetDlgItem (IDC_EDIT1)->GetDC ();
a=dlgFontDlg.m_cf.rgbColors;
dlgFontDlg.GetCurrentFont(&lf);
font.DeleteObject();
font.CreateFontIndirect(&lf);
GetDlgItem(IDC_EDIT1)->SetFont(&font);
pDC->SetTextColor (a);
我这个是Text控件,不是RichText控件,你看行不行
LOGFONT lf;
GetDlgItem(IDC_EDIT1)->GetFont()->GetLogFont(&lf);
//使用按钮的当前字体初始化字体对话框
CFontDialog dlgFontDlg(&lf);
//显示字体选择对话框
if (dlgFontDlg.DoModal() == IDOK)
{
//如果用户在字体选择对话框中单击了“确定”按钮
//则将按钮IDC_EDIT1的标题文本字体设置为所选定的字体
static CFont font;
COLORREF a;
CWnd* pWnd=GetDlgItem(IDC_EDIT1);
CDC* pDC=GetDlgItem (IDC_EDIT1)->GetDC ();
a=dlgFontDlg.m_cf.rgbColors;
dlgFontDlg.GetCurrentFont(&lf);
font.DeleteObject();
font.CreateFontIndirect(&lf);
GetDlgItem(IDC_EDIT1)->SetFont(&font);
pDC->SetTextColor (a);
我这个是Text控件,不是RichText控件,你看行不行
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//获得控件的当前字体
LOGFONT lf;
GetDlgItem(IDC_EDIT1)->GetFont()->GetLogFont(&lf);
//使用按钮的当前字体初始化字体对话框
CFontDialog dlgFontDlg(&lf);
//显示字体选择对话框
if (dlgFontDlg.DoModal() == IDOK)
{
//如果用户在字体选择对话框中单击了“确定”按钮
//则将按钮IDC_EDIT1的标题文本字体设置为所选定的字体
static CFont font;
COLORREF a;
CWnd* pWnd=GetDlgItem(IDC_EDIT1);
CDC* pDC=GetDlgItem (IDC_EDIT1)->GetDC ();
a=dlgFontDlg.m_cf.rgbColors;
dlgFontDlg.GetCurrentFont(&lf);
font.DeleteObject();
font.CreateFontIndirect(&lf);
GetDlgItem(IDC_EDIT1)->SetFont(&font);
pDC->SetTextColor (a);
LOGFONT lf;
GetDlgItem(IDC_EDIT1)->GetFont()->GetLogFont(&lf);
//使用按钮的当前字体初始化字体对话框
CFontDialog dlgFontDlg(&lf);
//显示字体选择对话框
if (dlgFontDlg.DoModal() == IDOK)
{
//如果用户在字体选择对话框中单击了“确定”按钮
//则将按钮IDC_EDIT1的标题文本字体设置为所选定的字体
static CFont font;
COLORREF a;
CWnd* pWnd=GetDlgItem(IDC_EDIT1);
CDC* pDC=GetDlgItem (IDC_EDIT1)->GetDC ();
a=dlgFontDlg.m_cf.rgbColors;
dlgFontDlg.GetCurrentFont(&lf);
font.DeleteObject();
font.CreateFontIndirect(&lf);
GetDlgItem(IDC_EDIT1)->SetFont(&font);
pDC->SetTextColor (a);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询