Results 1 to 7 of 7

Thread: Custom fonts in application

  1. #1

    Custom fonts in application

    In response to Paul's question, I wanted to write a simple tutorial, how to use fonts that aren't installed in our system.

    1.) First of all, grab your favorite font from your favorite website.
    2.) Put it in your application's directory and find a convenient name for it (fx. "font.ttf"). Also, check what's the name of font you're about to use.
    3.) To start using this font, you have to call method called AddFontResource.
    [pascal]
    uses
    ShellAPI;

    procedure TForm1.FormCreate(Sender: TObject);
    begin
    AddFontResource('font.ttf');
    end;
    [/pascal]
    Also, we need to broadcast a message to all windows that we're using a new resource. It's a simple modification of the above snippet:
    [pascal]
    uses
    ShellAPI;

    procedure TForm1.FormCreate(Sender: TObject);
    begin
    AddFontResource('font.ttf');
    SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
    end;
    [/pascal]
    4.) Now, if you want to use it with a Memo control, simply call:
    [pascal]
    Memo1.Font.Name := 'My Font';
    [/pascal]
    5.) The last thing is to remove the font and tell every window we won't be using it anymore. This code clears things up:
    [pascal]
    uses
    ShellAPI;

    procedure TForm1.FormDestroy(Sender: TObject);
    begin
    RemoveFontResource('font.ttf');
    SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
    end;
    [/pascal]

    It's been a while since I last used this code, I'm sure it worked under XP, but I'm not sure if it will under Vista. I'd appreciate any feedback.

  2. #2

    Re: Custom fonts in application

    neat! nice one Brainer

    cheers,
    Paul

  3. #3

    Re: Custom fonts in application

    One of my application used most the same method, under vista, it works!


  4. #4

    Re: Custom fonts in application

    Thanks, Chien.

    Who's to say if it works under Windows 7? lol

  5. #5
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Re: Custom fonts in application

    Very cool. Is this with Delphi alone? Or will it also work with Lazarus?

    Would be quite useful if you wanted to distribute your own truetype fonts for your game, but didn't want to shove them into the users Windows/Fonts folder.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  6. #6

    Re: Custom fonts in application

    Nope, I think it'd work for Lazarus, too, hence those two procedures are consisted in Windows API.
    AddFontResource: http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
    RemoveFontResource: http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

    You might also consider using AddFontResourceEx and RemoveFontResourceEx. They allow to hide your font from other application, thus you don't have to use SendMessage to tell every other process there's a new font.

  7. #7

    Re: Custom fonts in application

    That method will work in Lazarus on Windows only. Some place I found a direct fon (universal font file) render for Lazarus but don't remember where now, and I'm too lazy to google around

Bookmarks

Posting Permissions

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