Solved

Install Font? HELP!!!

Posted on 2004-04-30
14
1,163 Views
Last Modified: 2013-12-03
Help Me!!!

I am trying to use the addfontresource api but am getting no results - the font does not appear in the font directory.

I am using this code (with Win XP):

AddFontResource(PChar('C:\SIL.ttf'));
  SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);

Could someone please tell me what I am doing wrong or suggest another method of installing and removing fonts!

Thanks :-)
0
Comment
Question by:siwiher
  • 5
  • 5
  • 3
  • +1
14 Comments
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10963861

You should be checking the error code:

var dwError: Integer;
begin

  dwError:=AddFontResource(PChar('C:\SIL.ttf'));
  if (dwError = 0) then
  begin
    dwError:=GetLastError;
   ShowMessage(SysErrorMessage(dwError));
 end;

... rest of code


Regards,
Russell

 
0
 

Author Comment

by:siwiher
ID: 10963890
Tried it - no error message and font was not installed!
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10963939

Very odd....

Can you check the return code of the call to see how many fonts were installed? (normally one).

Also, have you tried copying this font file to the appropriate folder (C:\WINDOWS\Fonts for XP, for example) and then try the call that way.

eg:

// copy file to c:\windows\fonts
if (AddFontResource(PChar('C:\windows\fonts\SIL.ttf')) = 0) then
  RaiseLastWin32Error
else
  SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);

------------

Russell

0
 

Author Comment

by:siwiher
ID: 10964013
The copy file command worked!

Would it work for other versions of windows i.e. 98, nt?

Also could you tell me how to get the current windows directory as it could be differnt from 'C:\Windows'

Thanks
0
 
LVL 3

Expert Comment

by:Kunfufaresi
ID: 10964090
Hello,

to tell you that, these are from a Delphi Jedi unit

function CheckLastChar(Value: string): string;
begin
  //Check if the last Char is a \, and add one if necessary
  if (Length(Value) > 0) and (Value[Length(Value)] <> '\') then
    Value := Value + '\';
  Result := Value;
end;

{******************************************************}

function GetWindowsDirectory: string;
begin
  Windows.GetWindowsDirectory(buf, 250);
  Result := CheckLastChar(buf);
end;

{******************************************************}

function GetSystemDirectory: string;
begin
  Windows.GetSystemDirectory(buf, 250);
  Result := CheckLastChar(buf);
end;

{******************************************************}

function GetTempPath: string;
begin
  Windows.GetTempPath(250, buf);
  Result := CheckLastChar(buf);
end;

{******************************************************}

the full unit has much more. Also there might be a way to link fonts without copying them to windows fonts folder i'll search for that.

Kunfu Faresi
0
 

Author Comment

by:siwiher
ID: 10964128
Thanks guys for all your help

>> Kunfu, it would be great if you could check that out for me.
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 100 total points
ID: 10964132

////////////////////////////////////////////////////////////////////////////
//   Copy the specified file to the %windir%\font path and install the font
//   Returns 0 on succes, any other number is a windows error code
////////////////////////////////////////////////////////////////////////////
function CopyAndInstallFont(FontFile: String): Integer;
var  lpWinDir:   Array [0..MAX_PATH] of Char;
begin

  // Get the windows directory
  ZeroMemory(@lpWinDir, MAX_PATH);
  GetWindowsDirectory(lpWinDir, MAX_PATH);

  // Copy the file name passed in to the path
  StrLCat(@lpWinDir, '\fonts\', MAX_PATH);
  StrLCat(@lpWinDir, PChar(ExtractFileName(FontFile)), MAX_PATH);

  // Make sure the file names arent the same
  if (StrIComp(@lpWinDir, PChar(FontFile)) <> 0) then
  begin
     if not(CopyFile(PChar(FontFile), @lpWinDir, True)) then
     begin
        // Failure
        result:=GetLastError;
        // Bail
        exit;
     end;
  end;

  // Install the font
  if (AddFontResource(@lpWinDir) = 0) then
     // Failure
     result:=GetLastError
  else
  begin
     // Notify other windows of the font change
     SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);
     // Success
     result:=ERROR_SUCCESS;
  end;

end;

usage:
---------------

dwError:=CopyAndInstallFont('C:\SIL.ttf');
if (dwError = 0) then
 ShowMessage('Success')
else
 ShowMessage(SysErrorMessage(dwError));


Russell

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 11

Expert Comment

by:shaneholmes
ID: 10964143
procedure TForm1.FormActivate(Sender: TObject);
var
 Dir: array[0..200] of Char;  // use MAXFILEPATH or something like thisinstead of 200
begin
  GetSystemDirectory(Dir, SizeOf(Dir)-1);
  ShowMessage(Dir);
end;

Shane
0
 
LVL 3

Expert Comment

by:Kunfufaresi
ID: 10964150
Hello,

you could try adding a key into registery at

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Fonts
or
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts
depending on your version

as a string value name as the name of the font and data name of file with full path. I've not tried this though, and it might need restart or maybe just SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0) might do the trick. Or nothing, as i remember windows works some time when you try to emurate the fonts so it might be looking into the registery every time.

Kunfu Faresi
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10964162

Actually, it should be  MAX_PATH which is defined in Windows.pas, and the value is  260.

Russell
0
 
LVL 3

Expert Comment

by:Kunfufaresi
ID: 10964165
btw buf is
    buf: array[0..250] of Char;
didnt copy that one.
0
 

Author Comment

by:siwiher
ID: 10964279
>> rllibby, thanks it worked very well. Just one last question before I give you the points: Will this work on other versions of windows?

>> to all others thank you very much for your help!
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 10964326
Yes, by using the GetWindowsDirectory (which is the variable part), it will build the correct path:

Just finished checking on the following OS'es:

Win95/98/Me/NT/2000/XP/2003

Regards,
Russell
0
 

Author Comment

by:siwiher
ID: 10964340
Thank you very much rllibby, I think you deserve 100 points!

Thank to others for their contributions.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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