Results 1 to 5 of 5

Thread: Using fonts in your project that are embedded in resources

  1. #1

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Lightbulb 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...



    To Implement:
    vb Code:
    1. 'At begining of project call this:
    2. clsText.installAllFontsFromResources()
    3.  
    4. 'To show something using the font from cache:
    5. Button1.Font = clsText.FontFromCache(New Drawing.Font("Font name", 72, Drawing.FontStyle.Bold))
    6.  
    7. '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:
    8. Button1.Font = clsText.FontFromCache(Button1.Font)
    9.  
    10. 'If you want to change all controls fonts on a form from cache you can do something like this:
    11. Dim ctl As Control = Me.GetNextControl(Me, True)
    12. Do Until ctl Is Nothing
    13.     ctl.Font = clsText.FontFromCache(ctl.Font)
    14.     ctl = Me.GetNextControl(ctl, True)
    15. 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 - Downloads
    *Downloads are now hosted on multiupload due to the attachment limit on VBForums

    Kris

  2. #2
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    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...)

  3. #3

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    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

  4. #4

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    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

  5. #5
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Using fonts in your project that are embedded in resources

    I don't think there's another way, no. It's too bad.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width