VB中用API實現字體公用對話框例子
2024-07-21 02:20:47
供稿:網友
 
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
private declare function choosefont lib "comdlg32.dll" alias "choosefonta" _
 (byref pchoosefont as choosefont) as long
private sub command1_click()
 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
end sub