0

I have fonts folder contains font with extension "*.ttf", I need to install all the fonts in the user's computer to use my programs with these fonts.

  • Or use a [PrivateFontCollection](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.text.privatefontcollection), loading Fonts from both Files or Project Resources. Some examples using both methods [here](https://stackoverflow.com/a/57231407/7444103). If you really want to add the Fonts to the system Fonts collection, then probably this: [How to install a Windows Font](https://stackoverflow.com/a/14796255/7444103) – Jimi Jul 30 '20 at 13:21
  • If you instead want to add your Fonts to the system Fonts only temporarily (while your Process is active), probably [AddFontMemResourceEx](https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-addfontmemresourceex). If you want the Fonts to persist, you have to add an entry to the Registry. [AddFontResourceEx](https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-addfontresourceexw) adds Fonts for the current User session that can be marked as `Private` instead. – Jimi Jul 30 '20 at 14:33
  • Note that the code in *How to install a Windows Font* is a copy/paste of some other Web resource that is missing the broadcast of `WM_FONTCHANGE` (call `SendMessage` with `hWnd` set to `HWND_BROADCAST`) – Jimi Jul 30 '20 at 14:34

1 Answers1

0

First You need Execute method to install font on Startup

  Public _pfc As PrivateFontCollection = Nothing

    Sub Installfont() 
        Try
            'INIT THE FONT COLLECTION
            _pfc = New PrivateFontCollection
            'LOAD MEMORY POINTER FOR FONT RESOURCE
            Dim fontMemPointer As IntPtr =
            Marshal.AllocCoTaskMem(My.Resources.MyFont.Length)
            'COPY THE DATA TO THE MEMORY LOCATION
            Marshal.Copy(My.Resources.MyFont, 0, fontMemPointer, My.Resources.MyFont.Length)
            'LOAD THE MEMORY FONT INTO THE PRIVATE FONT COLLECTION
            _pfc.AddMemoryFont(fontMemPointer, My.Resources.MyFont.Length)
            'FREE UNSAFE MEMORY
            Marshal.FreeCoTaskMem(fontMemPointer)
            Return True
        Catch ex As Exception
           MessageBox(ex.Message)

            Return False
        End Try
    End Sub

Second Method to Change Font in Form

 Sub Usefont(ByVal frm As Form)
            Dim toVisit As New List(Of Control) From {
                frm}
            toVisit.Add(frm)
            Do Until (toVisit.Count = 0)
                Dim cur As Control = toVisit(0)
                toVisit.RemoveAt(0)
                frm.Font = New Font(_pfc.Families(0), frm.Font.Size, FontStyle.Bold)
                For Each child As Control In cur.Controls
                    toVisit.Add(child)
                    child.Font = New Font(_pfc.Families(0), child.Font.Size, FontStyle.Bold)
                Next
            Loop
        End If
    End Sub