unit ButtonB;

interface

uses
  ButtonBase, Classes, LCLIntf, LCLType, SysUtils,
  {$IFDEF DPI}Dpi.Graphics{$ELSE}Graphics{$ENDIF};

type

  { TButtonB }

  TButtonB = class(TButtonBase)
  private
    FMask: TBitmap;
    FIndex: Integer;
    FBuffer: TBitmap;
    procedure SetIndex(Text: Integer);
  protected
    procedure Paint; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property Mask: TBitmap read FMask write FMask;
  published
    property Visible;
    property ButtonIndex: Integer read FIndex write SetIndex;
    property OnClick;
  end;

procedure Register;


implementation

uses
  ScreenTools;

procedure Register;
begin
  RegisterComponents('C-evo', [TButtonB]);
end;

constructor TButtonB.Create;
begin
  inherited;
  ShowHint := True;
  SetBounds(0, 0, 25, 25);
  FBuffer := TBitmap.Create;
  FBuffer.PixelFormat := TPixelFormat.pf24bit;
  FBuffer.SetSize(Width, Height);
  FBuffer.Canvas.FillRect(0, 0, FBuffer.Width, FBuffer.Height);
end;

destructor TButtonB.Destroy;
begin
  FreeAndNil(FBuffer);
  inherited;
end;

procedure TButtonB.Paint;
begin
  with Canvas do
    if FGraphic <> nil then begin
      UnshareBitmap(FBuffer);
      BitBltBitmap(FBuffer, 0, 0, Width, Height, FGraphic, 169,
        243 + 26 * Byte(FDown));
      if FIndex >= 0 then begin
        BitBltBitmap(FBuffer, 0, 0, Width, Height, FMask,
          1 + FIndex mod 12 * 26, 337 + FIndex div 12 * 26, SRCAND);
        BitBltBitmap(FBuffer, 0, 0, Width, Height, FGraphic,
          1 + FIndex mod 12 * 26, 337 + FIndex div 12 * 26, SRCPAINT);
      end;
      BitBltCanvas(Canvas, 0, 0, Width, Height, FBuffer.Canvas, 0, 0);
    end else begin
      Brush.Color := $0000FF;
      FrameRect(Rect(0, 0, Width, Height));
    end;
  inherited;
end;

procedure TButtonB.SetIndex(Text: Integer);
begin
  if Text <> FIndex then begin
    FIndex := Text;
    Invalidate;
  end;
end;

end.
