Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Install Font? HELP!!!

Posted on 2004-04-30
14
Medium Priority
?
1,190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

610 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