如何字体文件变成资源文件, 调用它,这样即便系统里面没有该字体,也能在程序中显示。   请各位帮帮忙!

解决方案 »

  1.   

    1. Create RC file (e.g MY.RC)
       MYFONT RCDATA "FONT_FILE_NAME"
    2. Compile RC file using BRCC32 MY.RC
    3. Add compiler directive {$R MY.RES}
    4. Use below code in OnCreate and OnDestroy events
    procedure TForm1.FormCreate(Sender: TObject);
    var
      res: TResourceStream;
    begin
      res := TResourceStream.Create(hInstance, 'MYFONT', RT_RCDATA);
      try
        res.SavetoFile('myfont.ttf');
      finally
          res.Free;
      end;
        AddFontResource(PChar('myfont.ttf'));
        SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);
      //Now you use your own font
      Label1.Font.Size:=24;
      Label1.Font.Name:='myfont';
    end;//We don't want this font to be available after our application is closedprocedure TForm1.FormDestroy(Sender: TObject; var Action: TCloseAction) ;
    begin
       RemoveFontResource('PATH_TO_MyFont.TTF') ;
       SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0) ;
    end;
    Remeber Font's can be directly used from memory. We have to save them to file and call AddFontResource API function. The AddFontResource function adds the font resource from the specified file to the system font table. We need to fake this font adding and we should broadcast WM_FONTCHANGE message.When we don't need this font we must call RemoveFontResource function to remove this resource.Hope it helpsThanks,Ali
      

  2.   

    Correction:Remember Font's can be SHOULD be read as Font's CAN'T be
      

  3.   

    Ali 啊 procedure TForm1.FormDestroy(Sender: TObject; var Action: TCloseAction) ;
    中  var Action: TCloseAction 部分 通不过 。屏蔽后 字体没有变化啊http://ishare.iask.sina.com.cn/f/14569194.html?w=' target='_blank'>字体资源.rar 我把工程文件传到网上了。 是 D7的
      

  4.   

    It is working :). Please change the font of label1 at design time to something different like "Arial". Run the application and you will see font is changed to Trana.ttf font (Transponder AOE).You will have to write the actual Font name instead of providing the ttf file name.
        Label1.Font.Size:=24;
        Label1.Font.Name:='Transponder AOE';
      

  5.   

    thank you very much !  I think the name of the font is so long, it is not the font name.
      
      but, it is.  
      
      thanks!
       
      

  6.   

    mwy654321o 这样啊,下次我随便问个问题看到你就算一个字的回复,就给你很高的分,希望你不要食言呦。我不会的。
      

  7.   

    Ali 啊  字体可以改变了,可是退出后字体文件没有被删除掉,望您赐教啊! procedure TForm1.FormDestroy(Sender: TObject; var Action: TCloseAction) ;
    中 var Action: TCloseAction 部分 通不过 。