码迷,mamicode.com
首页 > 其他好文 > 详细

添加字体

时间:2017-02-07 16:25:50      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:ring   broadcast   nbsp   win   pos   public   cli   creates   procedure   

uses Registry;

procedure TForm1.Button1Click(Sender: TObject);
var
  reg: TRegistry;
  b : bool;
begin
  CopyFile(‘C:\DOWNLOAD\FP000100.TTF‘,
           ‘C:\WINDOWS\FONTS\FP000100.TTF‘, b);
  reg := TRegistry.Create;
  reg.RootKey := HKEY_LOCAL_MACHINE;
  reg.LazyWrite := false;
  reg.OpenKey(‘Software\Microsoft\Windows\CurrentVersion\Fonts‘,
              false);
  reg.WriteString(‘TESTMICR (TrueType)‘,‘FP000100.TTF‘);
  reg.CloseKey;
  reg.free;
 {Add the font resource}
  AddFontResource(‘c:\windows\fonts\FP000100.TTF‘);
  SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
 {Remove the resource lock}
  RemoveFontResource(‘c:\windows\fonts\FP000100.TTF‘);
  SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
end;
//////////////////////////////////
type
  TForm1=class( TForm )
      procedure FormCreate(Sender: TObject);
      procedure FormDestroy(Sender: TObject);
      ...
    private
      { Private declarations }
      bLoadedFont: boolean;
    public
      { Public declarations }
  end;

procedure TForm1.FormCreate(Sender: TObject);

  var
    sAppDir: string;
    sFontRes: string;

  begin
  sAppDir := Application.ExeName;
  sAppDir := copy( sAppDir, 1, rpos( ‘\‘, sAppDir ) );

  sFontRes := sAppDir + ‘MYFONT.FOT‘;
  if not FileExists( sFontRes ) then
    begin
    sFontRes := sFontRes + #0;
    sFont := sAppDir + ‘MYFONT.TTF‘ + #0;
    CreateScalableFontResource( 0, @sFontRes[ 1 ], @sFont[ 1 ], nil );
    end;

  sFontRes := sAppDir + ‘MYFONT.FOT‘;
  if FileExists( sFontRes ) then
    begin
    sFontRes := sFontRes + #0;
    if AddFontResource( @sFontRes[ 1 ] ) = 0 then
      bLoadedFont := false
    else
      begin
      bLoadedFont := true;
      SendMessage( HWND_BROADCAST, WM_FONTCHANGE, 0, 0 );
      end;
    end;

  ...
  end;

procedure TForm1.FormDestroy(Sender: TObject);

  var
    sFontRes: string;

  begin
  if bLoadedFont then
    begin
    sFontRes := sAppDir + ‘MYFONT.FOT‘ + #0;
    RemoveFontResource( @sFontRes[ 1 ] );
    SendMessage( HWND_BROADCAST, WM_FONTCHANGE, 0, 0 );
    end;
  end;

添加字体

标签:ring   broadcast   nbsp   win   pos   public   cli   creates   procedure   

原文地址:http://www.cnblogs.com/yzryc/p/6374330.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!