_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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
code issue 8 97
Delphi: how to implement a User Shortcut mapper? 1 93
Create Database on Android via Delphi dbExpress 3 78
Create a path if not exists 7 67
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…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

932 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

10 Experts available now in Live!

Get 1:1 Help Now