ChooseFont包括名称,字体风格,字号等。
基本概念
函数原型:BOOL ChooseFont(LPCHOOSEFONT Ipcf);
参数:
Ipcf:指向一个含有初始化对话框信息的CHOOSEFONT结构。当返回ChooseFont函数时,此结构含有用户对字体选择的信息。
返回值:如果用户点击对话框的OK按钮,返回值为非零值,CHOOSEFONT结构中的成员表明用户的选择。如果用户取消或关闭Font对话框或出现错误信息,返回值为零。若想获得更多
错误信息。请调用CommDlgExtendedError函数,其返回值如下:
CDERR_FINDRESFAILURE;CDERR_NOHINSTANCE;CDERR_INITIALIZATION;CDERR_NOHOOK
CDERR_LOCKRESFAILURE;CDERR_NOTEMPLATE;CDERR_LOADRESFAILURE;
CDERR_STRUCTSIZE;CDERR_LOADSTRFAILURE;CDERR_MAXLESSTHANMIN
CDERR_MEMALLOCFAILURE;CDERR_NOFONTS;CDERR_MEMLOCKFAILURE
备注:可以为Font对话框提供一个CFHOOKProc挂钩程序。此挂钩程序能够处理发送给对话框的信息。
通过建立CHOOSEFONT结构中Flags成员的CE ENABLEHOOK标志和指定IPfn Hook成员中挂钩程序的地址可以使挂钩程序有效。
挂钩程序可以把信息WM_CHOOSEFONT_GETLOGLONT,WM_CHOOSEFONT_SETFLAGS和
WM_CHOOSEFONT_SETLOGFONT消息发送给对话框以便得到和创建当前值和对话框的图标。
速查:Windows NT:3.1及以一上版本;Windows:95及以上版本;Windows CE:不支持;头文件:Commdlg.h;库文件:comdlg32.lib;Unicode:在Windows NT环境中实现为Unicode和ANSI两个版本。
-----------------------------------------------------------------------------------------------------------------------------------
CHOOSEFONT
可用于上面ChooseFont来弹出对话框选择字体,
你做出的选择如果点击ok会保存在在传递给ChooseFont的
指针所指的
结构体中,
所以在调用完这个函数后可以通过分析该结构题的成员获得用户选择的字体信息,
来进行相应的客户区刷新等...
结构体原型
typedef struct tagCHOOSEFONTW
{
DWORD lStructSize; //指定这个结构的大小,以字节为单位
HWND hwndOwner; // 指向
所有者对话框窗口的句柄。这个成员可以是任意有效
窗口句柄,或如果对话框没有所有者它可以为NULL。
HDC hDC; // 显示的设备环境句柄,一般为NULL;
LPLOGFONTW lpLogFont; // 选中的字体返回值,这里的字体是逻辑字体
INT iPointSize; // 字体的大小
DWORD Flags; // 字体的位标记,用来初始化对话框。当对话框返回时,这些标记指出用户的输入。
COLORREF rgbColors; // 字体颜色
LPARAM lCustData; // 自定义数据,这数据是能被lpfnHook成员识别的系统传到的钩子程序
LPCFHOOKPROC lpfnHook; // 做钩子程序用的回调函数
LPCWSTR lpTemplateName; // 指向一个以空字符结束的字符串,字符串是对话框模板资源的名字,资源保存在能被hInstance成员识别的模块中
HINSTANCE hInstance; //实例句柄
LPWSTR lpszStyle; // 字体风格
WORD nFontType; // 字体类型
WORD ___MISSING_ALIGNMENT__;
INT nSizeMin; // 字体允许的最小尺寸
INT nSizeMax; //字体允许的最大尺寸
}
CHOOSEFONTW, *LPCHOOSEFONTW;
在VB中的应用
声明
Private Const LF_FACESIZE = 32
Private Const CF_PRINTERFONTS = &H2
Private Const CF_SCREENFONTS = &H1
Private Const CF_BOTH = (CF_SCREENFONTS Or CF_PRINTERFONTS)
Private Const CF_EFFECTS = &H100&
Private Const CF_FORCEFONTEXIST = &H10000
Private Const CF_INITTOLOGFONTSTRUCT = &H40&
Private Const CF_LIMITSIZE = &H2000&
Private Const REGULAR_FONTTYPE = &H400
'charset Constants
Private Const ANSI_CHARSET = 0
Private Const ARABIC_CHARSET = 178
Private Const BALTIC_CHARSET = 186
Private Const CHINESEBIG5_CHARSET = 136
Private Const DEFAULT_CHARSET = 1
Private Const EASTEUROPE_CHARSET = 238
Private Const GB2312_CHARSET = 134
Private Const GREEK_CHARSET = 161
Private Const HANGEUL_CHARSET = 129
Private Const HEBREW_CHARSET = 177
Private Const JOHAB_CHARSET = 130
Private Const MAC_CHARSET = 77
Private Const OEM_CHARSET = 255
Private Const RUSSIAN_CHARSET = 204
Private Const SHIFTJIS_CHARSET = 128
Private Const SYMBOL_CHARSET = 2
Private Const THAI_CHARSET = 222
Private Const TURKISH_CHARSET = 162
Private Type LOGFONT
lfHeight As Long
lfWidth As Long
lfEscapement As Long
lfOrientation As Long
lfWeight As Long
lfItalic As Byte
lfUnderline As Byte
lfStrikeOut As Byte
lfCharSet As Byte
lfOutPrecision As Byte
lfClipPrecision As Byte
lfQuality As Byte
lfPitchAndFamily As Byte
lfFaceName As String * 31
End Type
Private Type CHOOSEFONT
lStructSize As Long
hwndOwner As Long ' caller's window handle
hDC As Long ' printer DC/IC or NULL
lpLogFont As Long ' ptr. to a LOGFONT struct
iPointSize As Long ' 10 * size in points of selected font
flags As Long ' enum. type flags
rgbColors As Long ' returned text color
lCustData As Long ' data passed to hook fn.
lpfnHook As Long ' ptr. to hook function
lpTemplateName As String ' custom template name
hInstance As Long ' instance handle of.EXE that
' contains cust. dlg. template
lpszStyle As String ' return the style field here
' must be LF_FACESIZE or bigger
nFontType As Integer ' same value reported to the
EnumFonts' call back with the extra FONTTYPE_
' bits added
MISSING_ALIGNMENT As Integer
nSizeMin As Long ' minimum pt size allowed &
nSizeMax As Long ' max pt size allowed if
' CF_LIMITSIZE is used
End Type
函数的调用
假设需要改变字体的控件名称是Text1。
Dim cf As CHOOSEFONT, lfont As LOGFONT
Dim fontname As String, ret As Long
cf.flags = CF_BOTH Or CF_EFFECTS Or CF_FORCEFONTEXIST Or CF_INITTOLOGFONTSTRUCT Or CF_LIMITSIZE
cf.lpLogFont = VarPtr(lfont)
cf.lStructSize =
LenB(cf)
'cf.lStructSize = Len(cf) ' size of structure
cf.hwndOwner = Form1.hWnd ' window Form1 is opening this dialog box
'cf.hDC = Printer.hDC ' device context of default printer (using VB's mechanism)
cf.rgbColors = RGB(0, 0, 0) ' black
cf.nFontType = REGULAR_FONTTYPE ' regular font type i.e. not bold or anything
cf.nSizeMin = 10 ' minimum point size
cf.nSizeMax = 72 ' maximum point size
ret = CHOOSEFONT(cf) 'brings up the font dialog
If ret <> 0 Then ' success
fontname = StrConv(lfont.lfFaceName, vbUnicode, &H804) 'Retrieve chinese font name in english version os
fontname = Left$(fontname, InStr(1, fontname, vbNullChar) - 1)
'Assign the font properties to text1
With Text1.Font
.Charset = lfont.lfCharSet 'assign charset to font
.Name = fontname
.Size = cf.iPointSize / 10 'assign point size
End With
End If