Using fonts in your project that are embedded in resources
A while back I wanted a way to just put font files in my project resource and be able to use them in my project, so I came up with this:
Also keep in mind that I wrote this a while ago ... so this may be able to be done better...
http://img254.imageshack.us/img254/9...reenshothj.png
To Implement:
vb Code:
'At begining of project call this:
clsText.installAllFontsFromResources()
'To show something using the font from cache:
Button1.Font = clsText.FontFromCache(New Drawing.Font("Font name", 72, Drawing.FontStyle.Bold))
'If your Button1's font was already set to "Font Name" on your computer before deployment you should also be able to use something like:
Button1.Font = clsText.FontFromCache(Button1.Font)
'If you want to change all controls fonts on a form from cache you can do something like this:
Dim ctl As Control = Me.GetNextControl(Me, True)
Do Until ctl Is Nothing
ctl.Font = clsText.FontFromCache(ctl.Font)
ctl = Me.GetNextControl(ctl, True)
Loop
LIMITATIONS
Any form/control etc that has been created PRIOR to calling "clsText.installAllFontsFromResources()" will not be able to have their font set this way.. it is recommended that you create a class with a shared Main function in it if you wish to use this class (as can be seen in the example project)
Hope this helps some people ...
Also please provide feedback and say thanks if this helped you
LINK DOWN - http://i00productions.org/downloader...57474442345759 Downloads
*Downloads are now hosted on multiupload due to the attachment limit on VBForums
Kris
Re: Using fonts in your project that are embedded in resources
Nice, a couple of suggestions though...
- Don't use LCase(xItem.Name), use xItem.Name.ToLower(), and same with all the other LCases.
- You can save the entire hassle by using the My.Resources.* syntax and adding them to the collection manually at the start; maybe you don't have the convenience of automatically loading the fonts, but it will be several orders of magnitude faster (no reflection, no exceptions, no casting, interop...)
Re: Using fonts in your project that are embedded in resources
Yea i know about both of these ... made this a few years ago and haven't changed it since!
With your second point ... i did it this way because i didn't want to have to specify which resources were fonts and register them all individually ... would their be another way to pick up if they were a font, and if they are then add them, thus avoiding the issue?
Kris
Re: Using fonts in your project that are embedded in resources
Updated... with a test project - also changed the lcase's to .tolower's :)
Kris
Re: Using fonts in your project that are embedded in resources
I don't think there's another way, no. It's too bad.