Graphic Delphi

Title: Multiple bitmaps button
Question: Create a button using multiple bitmaps from a resource file.
Answer:
I've created a simple button that shows the bitmap DOOROPEN.bmp when the mouse is pressed, otherwise it displays DOORCLOSED.bmp. The bitmaps are kept in a resource file so using it multiple times in an application will not require extra bytes in the executable.
Just create a resource file named MyCloseBtn.res with two buttons called CLOSE (DOORCLOSED.bmp) and OPEN (DOOROPEN.bmp).
unit MyCloseBtn;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Buttons;
const
STR_CLOSEBTN = 'CLOSE';
STR_OPENBTN = 'OPEN';
type
TMyCloseBtn = class(TSpeedButton)
private
procedure MyMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure MyMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
protected
public
constructor Create(AOwner: TComponent); override;
published
end;
procedure Register;
implementation
{$R *.res}
procedure Register;
begin
RegisterComponents('Samples', [TMyCloseBtn]);
end;
{ TMyCloseBtn }
constructor TMyCloseBtn.Create(AOwner: TComponent);
begin
inherited;
Self.NumGlyphs := 2;
OnMouseUp := MyMouseUp;
OnMouseDown := MyMouseDown;
if (csDesigning in ComponentState) then
begin
Flat := true;
Caption := 'CLOSE';
Width := 55;
Height := 45;
Layout := blGlyphTop;
end
else
begin
Glyph.LoadFromResourceName(HInstance , STR_CLOSEBTN);
invalidate;
end;
end;
procedure TMyCloseBtn.MyMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Glyph.LoadFromResourceName(HInstance , STR_OPENBTN);
invalidate;
end;
procedure TMyCloseBtn.MyMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Glyph.LoadFromResourceName(HInstance , STR_CLOSEBTN);
invalidate;
end;
end.