VC 如何获取一个控件内的字体

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VC 如何获取一个控件内的字体相关的知识,希望对你有一定的参考价值。

我在对话框上放了一个RichText控件,在打开字体对话框的时候,能自动获取该控件的字体,并显示!我该如何做呢?

参考技术A 首先了解一下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

VC怎么能获取系统字体

通过GetStockObject函数可以取得系统字体。
The GetStockObject function retrieves a handle to one of the stock pens, brushes, fonts, or palettes.

HGDIOBJ GetStockObject(
int fnObject // stock object type
);
其中fnObject可以是以下字体:

ANSI_FIXED_FONT Windows fixed-pitch (monospace) system font.
ANSI_VAR_FONT Windows variable-pitch (proportional space) system font.
DEVICE_DEFAULT_FONT Windows NT/2000/XP: Device-dependent font.
DEFAULT_GUI_FONT Default font for user interface objects such as menus and dialog boxes. This is MS Sans Serif. Compare this with SYSTEM_FONT.
OEM_FIXED_FONT Original equipment manufacturer (OEM) dependent fixed-pitch (monospace) font.
SYSTEM_FONT System font. By default, the system uses the system font to draw menus, dialog box controls, and text.
Windows 95/98 and Windows NT: The system font is MS Sans Serif.

Windows 2000/XP: The system font is Tahoma

SYSTEM_FIXED_FONT Fixed-pitch (monospace) system font. This stock object is provided only for compatibility with 16-bit Windows versions earlier than 3.0.

参考资料:MSDN

参考技术A 你说的系统字体指的是窗口视默认使用的字体的话,可以用以下方法获得:
在View类里
CFont *f= this->GetFont();
f就是当前窗口视默认使用的字体对象指针
参考技术B VC 获取已系统安装的字体
BOOL CALLBACK EnumFonts(CONST LOGFONT* lplf, CONST TEXTMETRIC *lptm,DWORD dwType,LPARAM lparam)

CString tempFontName(lplf->lfFaceName);
CString temp=gSysFontNames;
if(tempFontName.Find("@")!=-1)
return true;
//可对tempFontName自行附加过滤条件
//
gSysFontNames.Format("%s,%s",temp,tempFontName);
//
return true;

void CDialogTest:OnButton1()

AfxMessageBox(GetSystemFontName((LPVOID)this);

CString CDialogTest::GetSysFontName(LPVOID lp)

gSysFontNames="";
try

CPaintDC dc((CWnd*)lp);
::EnumFonts(dc.m_hDC,NULL,(FONTENUMPROC)EnumFonts,0);
return gSysFontNames.Mid(1);

catch(...)

return "";

以上是关于VC 如何获取一个控件内的字体的主要内容,如果未能解决你的问题,请参考以下文章

如何获取控件句柄 vc

VC++生成OCX,如何获取其CLSID

vc6.0如何使用getwindowtext函数获取edit控件中的文本内容

Android如何获取TextView的控件宽度以及字体宽度

VC++6.0中如何获取combo box中的文本?

VC TAB控件子对话框如何使用主对话框的成员函数.