این هم سوس کد PrivateFontProvider:
Imports System.ComponentModel, System.Drawing.Text
<ProvideProperty("PrivateFont", GetType(Component))>
<ProvideProperty("PrivateFontSize", GetType(Component))>
<ProvideProperty("PrivateFontStyle", GetType(Component))>
Public Class PrivateFontProvider
Implements ISupportInitialize
Implements IExtenderProvider
Private PrivateFontsValue As New PrivateFontCollection
<Browsable(False)>
Public ReadOnly Property PrivateFonts() As PrivateFontCollection
Get
Return PrivateFontsValue
End Get
End Property
Private FontDirectoryValue As String = ""
Public Property FontDirectory() As String
Get
Return FontDirectoryValue
End Get
Set(ByVal value As String)
FontDirectoryValue = value
End Set
End Property
Protected Friend ComponentFonts As New Dictionary(Of Component, String)
Protected Friend ComponentFontSizes As New Dictionary(Of Component, Single)
Protected Friend ComponentFontStyles As New Dictionary(Of Component, FontStyle)
Public Sub SetPrivateFont(Component As Component, Name As String)
If ComponentFonts.ContainsKey(Component) Then
ComponentFonts(Component) = Name
ResetFont(Component, Name)
Else
ComponentFonts.Add(Component, Name)
End If
End Sub
Public Sub SetPrivateFontSize(Component As Component, Optional Size As Single = 11)
If ComponentFontSizes.ContainsKey(Component) Then
ComponentFontSizes(Component) = Size
ResetFont(Component, ComponentFonts(Component))
Else
ComponentFontSizes.Add(Component, Size)
End If
End Sub
Public Sub SetPrivateFontStyle(Component As Component, Style As FontStyle)
If ComponentFontStyles.ContainsKey(Component) Then
ComponentFontStyles(Component) = Style
ResetFont(Component, ComponentFonts(Component))
Else
ComponentFontStyles.Add(Component, Style)
End If
End Sub
Public Function GetPrivateFont(Component As Component) As String
If ComponentFonts.ContainsKey(Component) Then
Return ComponentFonts(Component)
End If
Return ""
End Function
Public Function GetPrivateFontSize(Component As Component) As Single
If ComponentFontSizes.ContainsKey(Component) Then
Return ComponentFontSizes(Component)
End If
Return 11
End Function
Public Function GetPrivateFontStyle(Component As Component) As FontStyle
If ComponentFontStyles.ContainsKey(Component) Then
Return ComponentFontStyles(Component)
End If
Return FontStyle.Regular
End Function
Public Sub EndInit() Implements System.ComponentModel.ISupportInitialize.EndInit
'If Me.Fonts.Count = 0 Then Exit Sub
If Me.FontDirectory.Length = 0 Then Exit Sub
If IO.Directory.Exists(Me.FontDirectory) = False Then Exit Sub
For Each File As String In IO.Directory.GetFiles(Me.FontDirectory, "*.ttf")
Me.PrivateFonts.AddFontFile(File)
Application.DoEvents()
Next
For Each ComponentFont As KeyValuePair(Of Component, String) In Me.ComponentFonts
ResetFont(ComponentFont.Key, ComponentFont.Value)
Next
End Sub
Sub ResetFont(Component As Component, FontName As String)
Dim FontSize As Single = Me.ComponentFontSizes(Component)
Dim FontStyle As FontStyle = Me.ComponentFontStyles(Component)
If TypeOf (Component) Is ToolStrip Then
DirectCast(Component, ToolStrip).Font =
New Font(FontName, FontSize, FontStyle)
ElseIf TypeOf (Component) Is Control Then
DirectCast(Component, Control).Font =
New Font(FontName, FontSize, FontStyle)
End If
End Sub
Public Function CanExtend(extendee As Object) As Boolean Implements System.ComponentModel.IExtenderProvider.CanExtend
Return CBool(TypeOf (extendee) Is ToolStrip Or TypeOf (extendee) Is Control)
End Function
Public Sub BeginInit() Implements System.ComponentModel.ISupportInitialize.BeginInit
End Sub
End Class
طریقه استفاده یک Component Class با نام privateFontProvider ایجاد و کد های مربوطه رو جایگزین کرده و پرژو Build شود....
سورس پروژه در پست اول موجود است....
Screenshot 2024-11-25 175744.png
Screenshot 2024-11-25 183818.png
Screenshot 2024-11-25 184000.png