基本概念
函式原型: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 Declare Function CHOOSEFONT Lib "comdlg32.dll" Alias "ChooseFontA" (ByRef pChoosefont As CHOOSEFONT) As Long
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
Text1.Text = .Name & ":" & .Charset & ":" & .Size 'display data in chosen Font
End With
End If