_Using my own FONT file without having it in the Windows\Font directory !

Posted on 1998-09-19
Last Modified: 2013-12-03
Hi, I would like to know how to map my program to a specific FONT file (*.ttf) which is in my *.exe directory but not registered to the Windows system.

I'm using Delphi 3 standard.

I'm doing this because i have a DBCS font (4 MB) and, while I am distributing the program from a CD, I don't want the user to install it(the font file) then remove it.......

Your help will be appreciated.

Question by:Wein

Accepted Solution

erajoj earned 60 total points
ID: 1340296
Here's an example of how to do it.
I haven't found a good way to retrieve the font's face name so I give you this, working but, lame solution. Here's what happens:

1. Enumeration of existing screen fonts.
2. Loading of new font(s)(more than one font can reside in a font file).
3. New enumeration of screen fonts.
4. Removal of old font names.

function EnumFontsProc(var LogFont: TLogFont; var TextMetric: TTextMetric;
  FontType: Integer; Data: Pointer): Integer; export;
  {$IFDEF WIN32} stdcall; {$ENDIF}
  TStrings( Data ).Add( StrPas( LogFont.lfFaceName ) );
  Result := 1;

function TForm1.LoadFont( sFontFilename: string; NewFontNames: TStrings ): Boolean;
  iOldFontCount   : Integer;
  rgsFontNames    : TStringList;
  Proc            : TFarProc;
  Result := False;
  Proc   := MakeProcInstance( @EnumFontsProc, hInstance );
  if FileExists( sFontFileName ) then
    rgsFontNames := TStringList.Create;
    {1} EnumFonts( GetDC( 0 ), nil, Proc, Pointer( rgsFontNames ) ); // get all current font names
    iOldFontCount := rgsFontNames.Count; rgsFontNames.Clear;     // no need for these anymore
    {2} if AddFontResource( PChar( sFontFileName ) ) <> 0 then
      Result := True;
      SendMessage( HWND_BROADCAST, WM_FONTCHANGE, 0, 0 );
      {3} EnumFonts( GetDC( 0 ), nil, Proc, Pointer( rgsFontNames ) ); // get all current font names
      {4} for iOldFontCount := 1 to iOldFontCount do rgsFontNames.Delete( 0 ); // delete all old names, new names are last
      if Assigned( NewFontNames ) then NewFontNames.AddStrings( rgsFontNames ); // copy them if possible

function TForm1.UnLoadFont( sFontFilename: string ): Boolean;
  Result := RemoveFontResource( PChar( sFontFilename ) );

And here's how to use it:
  LoadFont( ExtractFilePath( ParamStr( 0 ) ) + 'myfont.ttf', ListBox1.Items );

Don't forget UnLoadFont( ExtractFilePath( ParamStr( 0 ) ) + 'myfont.ttf' ) in your destructor or whereever you want it.

/// John

Author Comment

ID: 1340297
This is really good and works quickly.

I'm wondering if I use it with a 5MB font, what will happen.
Well, thanks for you help.

When you load the file, it registers to the system without copying the file, is that right?

If I forget unloading the font, what will happen?

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now