Results 1 to 2 of 2

Thread: [VB6] Registering Fonts Through VB

  1. #1

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    [VB6] Registering Fonts Through VB

    Hi all,

    I am shure I have seen this asked before so, I came up with a little function to do this. This is in two parts, the definitions used for the API calls and then the function its self.

    VB Code:
    1. ' These are the definitions. Put these at the top of your form.
    2. Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" ( _
    3.     ByVal hwndOwner As Long, _
    4.     ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
    5. Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" ( _
    6.     ByVal pidl As Long, _
    7.     ByVal pszPath As String) As Long
    8. Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
    9.     ByVal hwnd As Long, _
    10.     ByVal wMsg As Long, _
    11.     ByVal wParam As Long, _
    12.     lParam As Any) As Long
    13. Private Declare Function AddFontResource Lib "gdi32.dll" Alias "AddFontResourceA" ( _
    14.     ByVal lpFileName As String) As Long
    15.  
    16. Private Const CSIDL_FONTS = &H14
    17. Private Const HWND_BROADCAST = &HFFFF&
    18. Private Const WM_FONTCHANGE = &H1D
    19.  
    20. Private Type ITEMID
    21.     cb As Long
    22.     abID As Byte
    23. End Type
    24. Private Type ITEMIDLIST
    25.     mkid As ITEMID
    26. End Type
    27.  
    28. Private Function GetSpecialFolder(lngCSIDL As Long) As String
    29.     Dim lngRet As Long
    30.     Dim strPath As String
    31.     Dim IDL As ITEMIDLIST
    32.     lngRet = SHGetSpecialFolderLocation(100, lngCSIDL, IDL)
    33.     If lngRet = 0 Then
    34.         strPath = Space$(512)
    35.         lngRet = SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal strPath)
    36.         GetSpecialFolder = Left$(strPath, InStr(strPath, Chr$(0)) - 1)
    37.         Exit Function
    38.     End If
    39.     GetSpecialFolder = ""
    40. End Function

    VB Code:
    1. ' This is the function.
    2. Private Function RegisterFont(strFontLocation As String) As Boolean
    3.     Dim lngRet As Long
    4.     Dim intLen As Integer
    5.     Dim strFonts As String
    6.     Dim strParts() As String
    7.     If Dir$(strFontLocation) <> "" Then
    8.         strFonts = GetSpecialFolder(CSIDL_FONTS) & "\"
    9.         strParts = Split(strFontLocation, "\")
    10.         intLen = UBound(strParts)
    11.         FileCopy strFontLocation, strFonts & strParts(intLen)
    12.         lngRet = AddFontResource(strFonts & strParts(intLen))
    13.         If lngRet > 0 Then
    14.             SendMessage HWND_BROADCAST, WM_FONTCHANGE, 0, 0
    15.             RegisterFont = True ' Success
    16.          Else
    17.              RegisterFont = False ' Error
    18.          End If
    19.     Else
    20.         RegisterFont = False ' Error
    21.         Exit Function
    22.     End If
    23. End Function

    One warning, you must validate all inputs before you use this or it may reak the code and give an error.

    Any problems, just post here


    Edit: Added a BAS file (Attatched) to add more features. Add and remove fonts now supported also some checking has been preformed

    Cheers,

    RyanJ
    Attached Files Attached Files
    Last edited by sciguyryan; Jun 12th, 2005 at 05:17 PM.
    My Blog.

    Ryan Jones.

  2. #2
    New Member
    Join Date
    Jun 2008
    Posts
    1

    Re: [VB6] Registering Fonts Through VB

    Hi trying to register a font which is going to be used by my application and the user.
    I have my script exactly doing the same as you have mentioned.

    The problem I am facing is that when ever I register a font, though it can be seen along with list of other fonts, it can not be activated by choosing manually by the user. But my application can access the font and use it appropriately. The only way the user can use the registered font is by restarting the computer. This is not the case when manually place the font at windows/fonts folder.

    Can some one help me understand what is going wrong and help me fix my problem. Your help is greatly appreciated. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width