複製鏈接
請複製以下鏈接發送給好友

ChooseFont

鎖定
ChooseFont包括名稱,字體風格,字號等。
中文名
ChooseFont
包    括
名稱,字體風格,字號等
功    能
創建用户選擇邏輯字體屬性對話框
函數原型
BOOL ChooseFont
參    數
Ipcf,返回值等
備    註
為Font話框供CFHOOKProc掛鈎程序

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;

ChooseFont在VB中的應用

ChooseFont聲明

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

ChooseFont函數的調用

假設需要改變字體的控件名稱是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