Resource

Include font from from a resource file

Posted in Resource

You should include {$R MyNewFont.RES} line to the implementation section. For causing from a resource font is necessary to create an object of TResorceStream type and to add font with AddFontResource procedure. And for including font you should use the WM_FONTCHANGE message. There is a 'MYFONT' section which contents a font file in the resource file.

{$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;

Static linking of DLL

Posted in Resource

This example shows, how to create a function, which is started from dll. This is a static linking of dll.

/*  Code of the library */
library MyDLL1;

uses
  SysUtils,
  Classes;

function Max(i,j: Integer): Integer; export;
begin
  if i>=j then Result:=i
          else Result:=j;
end;

exports
  Max(i,j:Integer): Integer name 'MAX';

begin
end.


/* Code of the unit */
function Max(i,j: Integer): Integer;external 'MyDLL1' name 'MAX';

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Text:=IntToStr(Max(5, 10));
end;

Play wav sound from a resource file

Posted in Resource

The problem is not so difficult. You should include {$R MySoundRes.RES} line to the implementation section. And for playing wav sound you must use the PlaySound procedure. There is a 'SOUND' section which contents a musical file in the resource file.

{$R MySoundRes.RES}
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  PlaySound('SOUND', 0, SND_RESOURCE);
end;

Load bitmap from resource DLL

Posted in Resource

First of all, you should load the library with LoadLibrary function. And for loading bitmap from this library use LoadFromResourceName function. There is a 'BITMAP3' section which contents a bitmap file in the resource file.

procedure TForm1.Button1Click(Sender: TObject);
var
  LibHandle: THandle;
  Bitmap: Tbitmap;
begin
  LibHandle:=Loadlibrary('MyDLL.dll');
  try
    if LibHandle<>0 then
    begin
      Bitmap:=TBitmap.Create;
      Bitmap.LoadFromResourceName(LibHandle,'BITMAP3');
      Image1.Canvas.Draw(0,0,Bitmap);
    end;
  finally
    Bitmap.Free;
  end;
end;

Include jpeg as a resource in *.exe

Posted in Resource

For including jpeg as a resource in *.ュ� the construction {$R MyRes.RES} is necessary. For causing from a resource jpeg is necessary to create an object of TResorceStream type and to load jpeg with LoadFromStream procedure. There is a 'HELP' section which contents a jpeg file in the resource file.

{$R MyRes.RES}
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  MyRS: TResourceStream;
begin
  try
    MyRS:=TResourceStream.Create(HInstance, 'HELP', RT_RCDATA);
    Image1.Picture.LoadFromFile('example.jpg');
    if Image1.Picture.Graphic is TJPEGImage then
      TJPEGImage(Image1.Picture.Graphic).LoadFromStream(MyRS);
  finally
    MyRS.Free;
  end;
end;