2021年12月20日 星期一

use external fonts font directly from resources in Delphi Resource Compiler brcc32 MSBuild

 use external fonts font directly from resources in Delphi
AddFontMemResourceEx
https://docs.microsoft.com/zh-tw/windows/win32/api/wingdi/nf-wingdi-addfontmemresourceex?redirectedfrom=MSDN
https://docs.microsoft.com/zh-tw/windows/win32/api/wingdi/nf-wingdi-addfontresourcea?redirectedfrom=MSDN
https://docs.microsoft.com/zh-tw/windows/win32/api/wingdi/nf-wingdi-addfontresourceexa?redirectedfrom=MSDN
https://docs.microsoft.com/zh-tw/windows/win32/api/wingdi/nf-wingdi-addfontmemresourceex?redirectedfrom=MSDN
https://sourceforge.net/projects/jvcl/
 JVCL's TjvDataEmbedded AddFontResource
https://docwiki.embarcadero.com/RADStudio/Sydney/en/Step_3_-_Add_Style-Resources_as_RCDATA_(Delphi)
Step 3 - Add Style-Resources as RCDATA (Delphi)


procedure TForm1.FormCreate(Sender: TObject) ;
begin
  AddFontResource('c:\FONTS\MyFont.TTF') ;
  SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0) ;
end;

//Before application terminates we must remove our font:
procedure TForm1.FormDestroy(Sender: TObject) ;
begin
  RemoveFontResource('C:\FONTS\MyFont.TTF') ;
  SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0) ;
end;
https://stackoverflow.com/questions/2984474/embedding-a-font-in-delphi
https://stackoverflow.com/questions/36224624/loading-font-from-resource-file

TResourceStream AddFontResource WM_FONTCHANGE message
   ResStream : tResourceStream;
   FontsCount : integer;
   hFont : tHandle;
 
   ResStream := tResourceStream.Create(hInstance, ResourceName, RT_RCDATA);
   hFont := AddFontMemResourceEx(ResStream.Memory, ResStream.Size, nil, @FontsCount);
   result := (hFont <> 0);
   ResStream.Free();
 

function LoadResourceFontByName( const ResourceName : string; ResType: PChar ) : Boolean;
var
  ResStream : TResourceStream;
  FontsCount : DWORD;
begin
  ResStream := TResourceStream.Create(hInstance, ResourceName, ResType);
  try
    Result := (AddFontMemResourceEx(ResStream.Memory, ResStream.Size, nil, @FontsCount) <> 0);
  finally
    ResStream.Free;
  end;
end;

function LoadResourceFontByID( ResourceID : Integer; ResType: PChar ) : Boolean;
var
  ResStream : TResourceStream;
  FontsCount : DWORD;
begin
  ResStream := TResourceStream.CreateFromID(hInstance, ResourceID, ResType);
  try
    Result := (AddFontMemResourceEx(ResStream.Memory, ResStream.Size, nil, @FontsCount) <> 0);
  finally
    ResStream.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  if LoadResourceFontByName('MyFont1', RT_RCDATA) then
    Label1.Font.Name := 'My Font Name 1';

  if LoadResourceFontByID(2, RT_FONT) then
    Label2.Font.Name := 'My Font Name 2';
end;

LoadResourceFontByName
delphi add font resource file LoadResourceFontByName
[Solved] Embedding a font in delphi - Code Redirect
https://www.sql.ru/forum/1254355/delphi-zagruzit-shrift-iz-resursov

{$R MyNewFont.RES}
...
procedure TForm1.FormCreate(Sender: TObject);
var
  MyResStream: TResourceStream;
begin
  MyResStream:=TResourceStream.Create(hInstance, 'MYFONT', RT_RCDATA);
  MyResStream.SavetoFile('Gen4.ttf');
  AddFontResource(PChar('Gen4.ttf'));
  SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);
  Label1.Font.Charset:=SYMBOL_CHARSET;
  Label1.Font.Size:=24;
  Label1.Font.Name:='Gen4';
end;

MSBuild.exe C:/Project1.dproj /t:Build /p:configutation=Debug /p:platform=Win32

https://docwiki.embarcadero.com/RADStudio/Sydney/en/BRCC32.EXE,_the_Resource_Compiler
Resource Compiler, brcc32.exe

https://delphi.cjcsoft.net/viewthread.php?tid=47317

{$R MyFont.res}
  Res : TResourceStream;
 
  Res := TResourceStream.Create(hInstance, 'MY_FONT', Pchar('ANYOL1'));
  Res.SavetoFile('Bauhs93.ttf');
  Res.Free;
  AddFontResource(PChar('Bauhs93.ttf'));
  SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);
 
  RemoveFontResource(PChar("Bauhs93.ttf"))
  SendMessage(HWND_BROADCAST,WM_FONTCHANGE,0,0);














沒有留言: