Solved

Install Font? HELP!!!

Posted on 2004-04-30
14
1,169 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
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.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

805 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