使用ChooseFont API 打开体对话框时默认的字体大小怎么设呢

解决方案 »

  1.   

    Option Explicit
    Const FW_NORMAL = 400
    Const DEFAULT_CHARSET = 1
    Const OUT_DEFAULT_PRECIS = 0
    Const CLIP_DEFAULT_PRECIS = 0
    Const DEFAULT_QUALITY = 0
    Const DEFAULT_PITCH = 0
    Const FF_ROMAN = 16
    Const CF_PRINTERFONTS = &H2
    Const CF_SCREENFONTS = &H1
    Const CF_BOTH = (CF_SCREENFONTS Or CF_PRINTERFONTS)
    Const CF_EFFECTS = &H100&
    Const CF_FORCEFONTEXIST = &H10000
    Const CF_INITTOLOGFONTSTRUCT = &H40&
    Const CF_LIMITSIZE = &H2000&
    Const REGULAR_FONTTYPE = &H400
    Const LF_FACESIZE = 32
    Const CCHDEVICENAME = 32
    Const CCHFORMNAME = 32
    Const GMEM_MOVEABLE = &H2
    Const GMEM_ZEROINIT = &H40Private Type CHOOSECOLOR
        lStructSize As Long
        hwndOwner As Long
        hInstance As Long
        rgbResult As Long
        lpCustColors As String
        flags As Long
        lCustData As Long
        lpfnHook As Long
        lpTemplateName As String
    End Type
    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 TypePrivate Declare Function CHOOSECOLOR Lib "comdlg32.dll" Alias "ChooseColorA" (pChoosecolor As CHOOSECOLOR) As Long
    Private Declare Function CHOOSEFONT Lib "comdlg32.dll" Alias "ChooseFontA" (pChoosefont As CHOOSEFONT) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
    Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
    Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
    Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As LongPrivate Sub Command1_Click()
        MsgBox ShowFont
    End SubPrivate Function ShowFont() As String
        Dim cf As CHOOSEFONT, lfont As LOGFONT, hMem As Long, pMem As Long
        Dim fontname As String, retval As Long
        lfont.lfHeight = 0  ' determine default height
        lfont.lfWidth = 0  ' determine default width
        lfont.lfEscapement = 0  ' angle between baseline and escapement vector
        lfont.lfOrientation = 0  ' angle between baseline and orientation vector
        lfont.lfWeight = FW_NORMAL  ' normal weight i.e. not bold
        lfont.lfCharSet = DEFAULT_CHARSET  ' use default character set
        lfont.lfOutPrecision = OUT_DEFAULT_PRECIS  ' default precision mapping
        lfont.lfClipPrecision = CLIP_DEFAULT_PRECIS  ' default clipping precision
        lfont.lfQuality = DEFAULT_QUALITY  ' default quality setting
        lfont.lfPitchAndFamily = DEFAULT_PITCH Or FF_ROMAN  ' default pitch, proportional with serifs
        lfont.lfFaceName = "Times New Roman" & vbNullChar  ' string must be null-terminated
        ' Create the memory block which will act as the LOGFONT structure buffer.
        hMem = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len(lfont))
        pMem = GlobalLock(hMem)  ' lock and get pointer
        CopyMemory ByVal pMem, lfont, Len(lfont)  ' copy structure's contents into block
        ' Initialize dialog box: Screen and printer fonts, point size between 10 and 72.
        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.lpLogFont = pMem   ' pointer to LOGFONT memory block buffer
        cf.iPointSize = 120  ' 12 point font (in units of 1/10 point)
        cf.flags = CF_BOTH Or CF_EFFECTS Or CF_FORCEFONTEXIST Or CF_INITTOLOGFONTSTRUCT Or CF_LIMITSIZE
        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
        ' Now, call the function.  If successful, copy the LOGFONT structure back into the structure
        ' and then print out the attributes we mentioned earlier that the user selected.
        retval = CHOOSEFONT(cf)  ' open the dialog box
        If retval <> 0 Then  ' success
            CopyMemory lfont, ByVal pMem, Len(lfont)  ' copy memory back
            ' Now make the fixed-length string holding the font name into a "normal" string.
            ShowFont = Left(lfont.lfFaceName, InStr(lfont.lfFaceName, vbNullChar) - 1)
            Debug.Print  ' end the line
        End If
        ' Deallocate the memory block we created earlier.  Note that this must
        ' be done whether the function succeeded or not.
        retval = GlobalUnlock(hMem)  ' destroy pointer, unlock block
        retval = GlobalFree(hMem)  ' free the allocated memory
    End Function