Back


Loading an Icon From A Resource File


For this example we use three TButtons and a TImage component.

First we create a .res file using Borlands Image Editor.

In Image Editor:
Go to File | New | Resource File.
Right-click in the Untitled1.res window and choose New | Icon.
Click OK in the Icon Properties dialog that pops-up.
Double-click on the Icon1 item in the tree list.
Draw your icon.
Now close the Icon1 (Untitled1.res) dialog.
Add another icon.

Right-click on the Icon1 item in the tree list and choose Rename.
Rename Icon1 to IconOne and then change Icon2 to IconTwo.

Go to File | Save.
Save your resource file as loadicon.res in the directory (folder) where you will save your Project in Delphi.



In Delphi:

Start a new Project.

Add three TButton components and a TImage component to your Form.

Add {$R loadicon.res} next to {$R *.DFM} after the implementation.

Add this code to Button1's OnClick event:

procedure TForm1.Button1Click(Sender: TObject);
begin
    Application.Icon.Handle := LoadIcon(hInstance, 'ICONONE');
    Image1.Picture.Icon.Handle := LoadIcon(hInstance, 'ICONONE');
end;

For Button2's OnClick event add this code:

procedure TForm1.Button2Click(Sender: TObject);
begin
    Application.Icon.Handle := LoadIcon(hInstance, 'ICONTWO');
    Image1.Picture.Icon.Handle := LoadIcon(hInstance, 'ICONTWO');
end;

And for the last button, Button3, add this code:

procedure TForm1.Button3Click(Sender: TObject);
begin
    Image1.Picture.Assign(nil); // Clear the image
end;

Run your Application and click on the Buttons.



 

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Image1: TImage;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var Form1: TForm1;
implementation {$R *.DFM} {$R loadicon.res}

procedure TForm1.Button1Click(Sender: TObject);
begin
   Application.Icon.Handle := LoadIcon(hInstance, 'ICONONE');
   Image1.Picture.Icon.Handle := LoadIcon(hInstance, 'ICONONE');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Application.Icon.Handle := LoadIcon(hInstance, 'ICONTWO');
  Image1.Picture.Icon.Handle := LoadIcon(hInstance, 'ICONTWO');
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  Image1.Picture.Assign(nil); // Clear the image
end;

end.
 

Download the Delphi 5 code

Back