通过 vb.net 实用地安装字体列表

Installing List of font Pragmatically by vb.net

我的字体文件夹包含扩展名为 "*.ttf" 的字体,我需要在用户计算机上安装所有字体才能使用我的程序使用这些字体。

首先你需要Execute方法在启动时安装字体

  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

第二种改变表格字体的方法

 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