none
Choosefont的应用 RRS feed

答案

  • 在.NET程序中调用要使用PInvoke服务,使用这个工具可以看到如何写.NET的函数声明和数据类型:

    http://blogs.msdn.com/vbteam/archive/2008/03/14/making-pinvoke-easy.aspx

    <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)>  _
    Public Structure tagCHOOSEFONTA
       
        '''DWORD->unsigned int
        Public lStructSize As UInteger
       
        '''HWND->HWND__*
        Public hwndOwner As System.IntPtr
       
        '''HDC->HDC__*
        Public hDC As System.IntPtr
       
        '''LPLOGFONTA->tagLOGFONTA*
        Public lpLogFont As System.IntPtr
       
        '''INT->int
        Public iPointSize As Integer
       
        '''DWORD->unsigned int
        Public Flags As UInteger
       
        '''COLORREF->DWORD->unsigned int
        Public rgbColors As UInteger
       
        '''LPARAM->LONG_PTR->int
        Public lCustData As Integer
       
        '''LPCFHOOKPROC
        Public lpfnHook As LPCFHOOKPROC
       
        '''LPCSTR->CHAR*
        <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)>  _
        Public lpTemplateName As String
       
        '''HINSTANCE->HINSTANCE__*
        Public hInstance As System.IntPtr
       
        '''LPSTR->CHAR*
        <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)>  _
        Public lpszStyle As String
       
        '''WORD->unsigned short
        Public nFontType As UShort
       
        '''WORD->unsigned short
        Public ___MISSING_ALIGNMENT__ As UShort
       
        '''INT->int
        Public nSizeMin As Integer
       
        '''INT->int
        Public nSizeMax As Integer
    End Structure

    '''Return Type: UINT_PTR->unsigned int
    '''param0: HWND->HWND__*
    '''param1: UINT->unsigned int
    '''param2: WPARAM->UINT_PTR->unsigned int
    '''param3: LPARAM->LONG_PTR->int
    <System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.StdCall)>  _
    Public Delegate Function LPCFHOOKPROC(ByVal param0 As System.IntPtr, ByVal param1 As UInteger, ByVal param2 As System.IntPtr, ByVal param3 As System.IntPtr) As UInteger

    <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)>  _
    Public Structure HWND__
       
        '''int
        Public unused As Integer
    End Structure

    <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)>  _
    Public Structure HDC__
       
        '''int
        Public unused As Integer
    End Structure

    <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet:=System.Runtime.InteropServices.CharSet.[Ansi])>  _
    Public Structure tagLOGFONTA
       
        '''LONG->int
        Public lfHeight As Integer
       
        '''LONG->int
        Public lfWidth As Integer
       
        '''LONG->int
        Public lfEscapement As Integer
       
        '''LONG->int
        Public lfOrientation As Integer
       
        '''LONG->int
        Public lfWeight As Integer
       
        '''BYTE->unsigned char
        Public lfItalic As Byte
       
        '''BYTE->unsigned char
        Public lfUnderline As Byte
       
        '''BYTE->unsigned char
        Public lfStrikeOut As Byte
       
        '''BYTE->unsigned char
        Public lfCharSet As Byte
       
        '''BYTE->unsigned char
        Public lfOutPrecision As Byte
       
        '''BYTE->unsigned char
        Public lfClipPrecision As Byte
       
        '''BYTE->unsigned char
        Public lfQuality As Byte
       
        '''BYTE->unsigned char
        Public lfPitchAndFamily As Byte
       
        '''CHAR[32]
        <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=32)>  _
        Public lfFaceName As String
    End Structure

    <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)>  _
    Public Structure HINSTANCE__
       
        '''int
        Public unused As Integer
    End Structure

    Partial Public Class NativeMethods
       
        '''Return Type: BOOL->int
        '''param0: LPCHOOSEFONTA->tagCHOOSEFONTA*
        <System.Runtime.InteropServices.DllImportAttribute("comdlg32.dll", EntryPoint:="ChooseFontA")>  _
        Public Shared Function ChooseFontA(ByRef param0 As tagCHOOSEFONTA) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean
        End Function
    End Class

    Best regards,
    Riquel
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    2009年11月5日 2:42
    版主

全部回复

  • 先    
    字体:lf.lfFaceName = StrConv(Text1.FontName, vbFromUnicode) & vbNullChar '初始化字体名称,需要从Unicode转换,须以空字符结尾
    大小:   lf.lfHeight = -MulDiv(Text1.FontSize, GetDeviceCaps(hdc, LOGPIXELSY), 72)
    MulDiv 的定义:
    Private Declare Function MulDiv Lib "kernel32" (ByVal nNumber As Long, ByVal nNumerator As Long, ByVal nDenominator As Long) As Long

    最后
    cf.lpLogFont = VarPtr(LF)
    2009年11月2日 5:14
  • 在.NET程序中调用要使用PInvoke服务,使用这个工具可以看到如何写.NET的函数声明和数据类型:

    http://blogs.msdn.com/vbteam/archive/2008/03/14/making-pinvoke-easy.aspx

    <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)>  _
    Public Structure tagCHOOSEFONTA
       
        '''DWORD->unsigned int
        Public lStructSize As UInteger
       
        '''HWND->HWND__*
        Public hwndOwner As System.IntPtr
       
        '''HDC->HDC__*
        Public hDC As System.IntPtr
       
        '''LPLOGFONTA->tagLOGFONTA*
        Public lpLogFont As System.IntPtr
       
        '''INT->int
        Public iPointSize As Integer
       
        '''DWORD->unsigned int
        Public Flags As UInteger
       
        '''COLORREF->DWORD->unsigned int
        Public rgbColors As UInteger
       
        '''LPARAM->LONG_PTR->int
        Public lCustData As Integer
       
        '''LPCFHOOKPROC
        Public lpfnHook As LPCFHOOKPROC
       
        '''LPCSTR->CHAR*
        <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)>  _
        Public lpTemplateName As String
       
        '''HINSTANCE->HINSTANCE__*
        Public hInstance As System.IntPtr
       
        '''LPSTR->CHAR*
        <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)>  _
        Public lpszStyle As String
       
        '''WORD->unsigned short
        Public nFontType As UShort
       
        '''WORD->unsigned short
        Public ___MISSING_ALIGNMENT__ As UShort
       
        '''INT->int
        Public nSizeMin As Integer
       
        '''INT->int
        Public nSizeMax As Integer
    End Structure

    '''Return Type: UINT_PTR->unsigned int
    '''param0: HWND->HWND__*
    '''param1: UINT->unsigned int
    '''param2: WPARAM->UINT_PTR->unsigned int
    '''param3: LPARAM->LONG_PTR->int
    <System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.StdCall)>  _
    Public Delegate Function LPCFHOOKPROC(ByVal param0 As System.IntPtr, ByVal param1 As UInteger, ByVal param2 As System.IntPtr, ByVal param3 As System.IntPtr) As UInteger

    <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)>  _
    Public Structure HWND__
       
        '''int
        Public unused As Integer
    End Structure

    <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)>  _
    Public Structure HDC__
       
        '''int
        Public unused As Integer
    End Structure

    <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet:=System.Runtime.InteropServices.CharSet.[Ansi])>  _
    Public Structure tagLOGFONTA
       
        '''LONG->int
        Public lfHeight As Integer
       
        '''LONG->int
        Public lfWidth As Integer
       
        '''LONG->int
        Public lfEscapement As Integer
       
        '''LONG->int
        Public lfOrientation As Integer
       
        '''LONG->int
        Public lfWeight As Integer
       
        '''BYTE->unsigned char
        Public lfItalic As Byte
       
        '''BYTE->unsigned char
        Public lfUnderline As Byte
       
        '''BYTE->unsigned char
        Public lfStrikeOut As Byte
       
        '''BYTE->unsigned char
        Public lfCharSet As Byte
       
        '''BYTE->unsigned char
        Public lfOutPrecision As Byte
       
        '''BYTE->unsigned char
        Public lfClipPrecision As Byte
       
        '''BYTE->unsigned char
        Public lfQuality As Byte
       
        '''BYTE->unsigned char
        Public lfPitchAndFamily As Byte
       
        '''CHAR[32]
        <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=32)>  _
        Public lfFaceName As String
    End Structure

    <System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)>  _
    Public Structure HINSTANCE__
       
        '''int
        Public unused As Integer
    End Structure

    Partial Public Class NativeMethods
       
        '''Return Type: BOOL->int
        '''param0: LPCHOOSEFONTA->tagCHOOSEFONTA*
        <System.Runtime.InteropServices.DllImportAttribute("comdlg32.dll", EntryPoint:="ChooseFontA")>  _
        Public Shared Function ChooseFontA(ByRef param0 As tagCHOOSEFONTA) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean
        End Function
    End Class

    Best regards,
    Riquel
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    2009年11月5日 2:42
    版主
  • VB6.0
    声明
    Public Declare Function MulDiv Lib "kernel32" (ByVal nNumber As Long, ByVal nNumerator As Long, ByVal nDenominator As Long) As Long
    Public Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
    Const   LOGPIXELSY   =   90

    完美显示,ok
    2009年11月10日 8:31