TBitBtnCopyImage (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example uses a special 4-in-1 64X16 bitmap for a TBitBtn. Such a bitmap can be created using an image editor. As stated in the Help for the TBitBtn class or TBitBtn Glyph property, this bitmap supplies images for the Up, Disabled, Clicked and Down button states (TButtonState) for this TBitBtn.

Notice that the left side of the arrow turns red when you click and hold on the button. Also, the arrow turns grey with a red stripe when you click BitBtn2 to disable BitBtn1. The bitmap can be installed on the TBitBtn using the Glyph property or the CopyImage method. CopyImage allows you to use graphics other than a bitmap.

The BitBtn1 properties have to be assigned at run time because is a descendant of TBitBtn. I did this to be able to use CopyImage, which is a protected method. Configuring the imagelist is usually done at design time, but for those not building the code project, the code is here and not hidden in design work.

Code

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, Buttons, ExtCtrls, ImgList, Menus;

type

  myBitBtn = class(TBitBtn)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TForm1 = class(TForm)
    BitBtn2: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  BitBtn1: myBitBtn;
  imList : TImageList;
  bmap: TBitmap;
  ico: TIcon;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  BitBtn1:= myBitBtn.Create(self);
  BitBtn1.Parent:= self;
  BitBtn1.Visible:= True;
  BitBtn1.Caption:= 'BitBtn1';
  BitBtn1.Height:= 137;
  BitBtn1.Width:= 185;
  BitBtn1.Top:= 100;
  BitBtn1.Left:= 100;
  BitBtn1.OnClick:= BitBtn1Click;
  imList := TImageList.Create(Self);
  imList.Height:= 32;
  imList.Width:= 128;
  BitBtn1.Images:= imList;
  bmap:= TBitmap.Create();
  ico:= TIcon.Create();
  ico.Height:= 32;
  ico.Width:= 128;

//  bmap.LoadFromFile('MYARROW2D.bmp');
  ico.LoadFromFile('MYARROW2D32.ico');
  imList.AddIcon(ico);
  BitBtn1.CopyImage(imList, 0);
//  BitBtn1.Glyph:= bmap;
  BitBtn1.NumGlyphs:= 4;
//  Repaint;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  bmap.Free;
  ico.Free;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  RNum: Integer;
begin
  RNum := Random(4);
  case RNum of
    0: BitBtn1.Layout := blGlyphLeft;
    1: BitBtn1.Layout := blGlyphRight;
    2: BitBtn1.Layout := blGlyphTop;
    3: BitBtn1.Layout := blGlyphBottom;
    else
      ShowMessage('Unhandled case');
  end;
//  BitBtn1.CopyImage(imList, RNum);
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
begin
  BitBtn1.Enabled:= not BitBtn1.Enabled;
end;

Uses