unit UWFlatButton;
//------------------------------------------------------------------------------
{$mode ObjFPC}{$H+}
interface
uses
Classes, Controls, SysUtils, Graphics, LCLType, LazarusPackageIntf,
BGRABitmap, BGRABitmapTypes, ImgList, UWSystem.SysUtils;
type
{ UWFlatButton }
TUWFlatButton = class(TGraphicControl)
private
FBMP : TBGRABitmap;
FHightlightColor : TColor;
FBorderColor : TColor;
FCheckedColor : TColor;
FIsOver : Boolean;
FMouseIsDown : Boolean;
FImageIndex : TImageIndex;
FImages : TCustomImageList;
procedure SetImageIndex(const AIndex: TImageIndex);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure DrawBuffer;
protected
procedure Paint; override;
procedure Loaded; override;
procedure MouseLeave; override;
procedure MouseEnter; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
published
property HightlightColor : TColor read FHightlightColor write FHightlightColor;
property BorderColor : TColor read FBorderColor write FBorderColor;
property CheckedColor : TColor read FCheckedColor write FCheckedColor;
property Images: TCustomImageList read FImages write FImages;
property ImageIndex: TImageIndex read FImageIndex write SetImageIndex default -1;
property Align;
property Anchors;
property Color;
property Cursor;
property Enabled;
property Width default 23;
property Height default 23;
property PopupMenu;
property OnClick;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
end;
// -----------------------------------------------------------------------------
implementation
//------------------------------------------------------------------------------
{ UWFlatButton }
// -----------------------------------------------------------------------------
constructor TUWFlatButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
SetBounds(Left, Top, 23, 23);
FHightlightColor := clBtnHighlight;
FBorderColor := clActiveCaption;
FCheckedColor := clInactiveBorder;
FIsOver := False;
FMouseIsDown := False;
FImageIndex := -1;
Color := clBtnFace;
FBMP := TBGRABitmap.Create(Width, Height, ColorToBGRA(Color));
end;
// -----------------------------------------------------------------------------
destructor TUWFlatButton.Destroy;
begin
FBMP.Free;
inherited;
end;
// -----------------------------------------------------------------------------
procedure TUWFlatButton.DrawBuffer;
begin
FBMP.Fill(Color);
if FIsOver and not FMouseIsDown then
FBMP.RoundRect(0, 0, Width, Height, 8, 8, FBorderColor, FHightlightColor)
else if FMouseIsDown then
FBMP.RoundRect(0, 0, Width, Height, 8, 8, FBorderColor, FCheckedColor);
if Assigned(FImages) and (FImageIndex >= 0) then
FImages.Draw(FBMP.Canvas, (Width - (FImages.Width)) div 2, (Height - (FImages.Height)) div 2, FImageIndex, Enabled);
FBMP.Draw(Canvas, 0, 0);
end;
// -----------------------------------------------------------------------------
procedure TUWFlatButton.Paint;
begin
Canvas.Lock;
try
FBMP.Draw(Canvas, 0, 0);
if csDesigning in ComponentState then Canvas.DrawFocusRect(ClientRect);
finally
Canvas.Unlock;
end;
end;
// -----------------------------------------------------------------------------
procedure TUWFlatButton.Loaded;
begin
inherited Loaded;
DrawBuffer;
end;
// -----------------------------------------------------------------------------
procedure TUWFlatButton.MouseLeave;
begin
inherited MouseLeave;
FIsOver := False;
DrawBuffer;
end;
// -----------------------------------------------------------------------------
procedure TUWFlatButton.MouseEnter;
begin
inherited MouseEnter;
FIsOver := True;
DrawBuffer;
end;
// -----------------------------------------------------------------------------
procedure TUWFlatButton.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited MouseDown(Button, Shift, X, Y);
if (ssLeft in Shift) then
begin
FMouseIsDown := True;
DrawBuffer;
end;
end;
//------------------------------------------------------------------------------
procedure TUWFlatButton.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited MouseUp(Button, Shift, X, Y);
if FMouseIsDown then
begin
FMouseIsDown := False;
DrawBuffer;
end;
end;
//------------------------------------------------------------------------------
procedure TUWFlatButton.SetImageIndex(const AIndex: TImageIndex);
begin
if FImageIndex <> AIndex then
begin
FImageIndex := AIndex;
DrawBuffer;
end;
end;
//------------------------------------------------------------------------------