I am having a Delphi XE Project with the following resource:
I have used function LoadResourceFont
and tried the following code:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
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 Custom Font1';
if LoadResourceFontByID(2, RT_FONT) then Label2.Font.Name := 'My Custom Font2';
end.
By the way: I know that I should have redistribution rights for the font if I need to embedded it into my EXE file.
But the problem is that if the font name ( I have used My Custom Font1, My Custom Font2) is not the actual name of the font, the font does not load properly and shows an unknown font character.
My question is that whether though I have used ResStream.Free
in the founctions, yet I have to define again FormClose
and FormDestroy
event to protect Memory Leakage or not?