[Import Custom Font's via Ressources]
This can come in handy when you want to use a custom font,
but your app needs to run without admin rights (because of ClickOnce or other least privileged requirements).
Without admin rights you cannot install fonts on a system.
With this tutorial allows you too import a font file straight from your applications ressources without any worries of user's needing the fonts installed!
1. Create Module
The Module Can be Named Whatever You Want it to be Named!
Module Created by :: MATTHEW KLEINWAKS
[img]http://www.winsocketdotnetworkprogramming.com/xmlwebservicesaspnetworkprogramming11chap_files/xmlwebservicesasprogramming215.png[/img]
2. Open and Replace the Module With the Following Code
Replace "DIGITALDREAMNARROW" With the Name of The Font Will be Using. ex: Open_Sans
Make Sure it's The Same Name (Case Sensitive) as The File You Will Be Creating in Step #3
3. Adding the Font File to Your Ressources, ex: Open_Sans
Make sure once again that it is the same name as in Step #2 (Case Sensitive)
I know there are multiple ways of adding resources, I prefer this method!
[img]http://static.daniweb.com/images/attachments/3/img1.jpg[/img]
[img]http://pad1.whstatic.com/images/thumb/8/83/Save-File-from-Your-Resources-in-Vb.Net-Step-6.jpg/550px-Save-File-from-Your-Resources-in-Vb.Net-Step-6.jpg[/img]
4. Change Font's on Your Labels!
NameOfClass.GetInstance(FONT_SIZE, FONT_TYPE)
This can come in handy when you want to use a custom font,
but your app needs to run without admin rights (because of ClickOnce or other least privileged requirements).
Without admin rights you cannot install fonts on a system.
With this tutorial allows you too import a font file straight from your applications ressources without any worries of user's needing the fonts installed!
1. Create Module
The Module Can be Named Whatever You Want it to be Named!
Module Created by :: MATTHEW KLEINWAKS
[img]http://www.winsocketdotnetworkprogramming.com/xmlwebservicesaspnetworkprogramming11chap_files/xmlwebservicesasprogramming215.png[/img]
2. Open and Replace the Module With the Following Code
Replace "DIGITALDREAMNARROW" With the Name of The Font Will be Using. ex: Open_Sans
Make Sure it's The Same Name (Case Sensitive) as The File You Will Be Creating in Step #3
Code
Imports System.Drawing.Text
Imports System.Runtime.InteropServices
Module CustomFont
'PRIVATE FONT COLLECTION TO HOLD THE DYNAMIC FONT
Private _pfc As PrivateFontCollection = Nothing
Public ReadOnly Property GetInstance(ByVal Size As Single, _
ByVal style As FontStyle) As Font
Get
'IF THIS IS THE FIRST TIME GETTING AN INSTANCE
'LOAD THE FONT FROM RESOURCES
If _pfc Is Nothing Then LoadFont()
'RETURN A NEW FONT OBJECT BASED ON THE SIZE AND STYLE PASSED IN
Return New Font(_pfc.Families(0), Size, style)
End Get
End Property
Private Sub LoadFont()
Try
'INIT THE FONT COLLECTION
_pfc = New PrivateFontCollection
'LOAD MEMORY POINTER FOR FONT RESOURCE
Dim fontMemPointer As IntPtr = _
Marshal.AllocCoTaskMem( _
My.Resources.DIGITALDREAMNARROW.Length)
'COPY THE DATA TO THE MEMORY LOCATION
Marshal.Copy(My.Resources.DIGITALDREAMNARROW, _
0, fontMemPointer, _
My.Resources.DIGITALDREAMNARROW.Length)
'LOAD THE MEMORY FONT INTO THE PRIVATE FONT COLLECTION
_pfc.AddMemoryFont(fontMemPointer, _
My.Resources.DIGITALDREAMNARROW.Length)
'FREE UNSAFE MEMORY
Marshal.FreeCoTaskMem(fontMemPointer)
Catch ex As Exception
'ERROR LOADING FONT. HANDLE EXCEPTION HERE
End Try
End Sub
End Module3. Adding the Font File to Your Ressources, ex: Open_Sans
Make sure once again that it is the same name as in Step #2 (Case Sensitive)
I know there are multiple ways of adding resources, I prefer this method!
[img]http://static.daniweb.com/images/attachments/3/img1.jpg[/img]
[img]http://pad1.whstatic.com/images/thumb/8/83/Save-File-from-Your-Resources-in-Vb.Net-Step-6.jpg/550px-Save-File-from-Your-Resources-in-Vb.Net-Step-6.jpg[/img]
4. Change Font's on Your Labels!
NameOfClass.GetInstance(FONT_SIZE, FONT_TYPE)
Code
Label1.Font = CustomFont.GetInstance(12, FontStyle.Bold)![[Image: wlQx51C.png]](https://i.imgur.com/wlQx51C.png)

