unit OButton3;

interface

uses
  SysUtils, Classes, Graphics, Controls;

const
  OColor: Array[1..3] of TColor = (clBlack, clGray, clWhite);

type
  TOButton = class(TCustomControl)
  private
    { Private-Deklarationen }
    x,y: Integer;
    FColor: TColor;
    FCaption: String;
    Pressed: Boolean;
  protected
    { Protected-Deklarationen }
    procedure Paint; override;
    procedure MouseDown
      (Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
    procedure MouseUp
      (Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
    procedure SetCaption (OText: String); virtual;
  public
    { Public-Deklarationen }
    constructor Create (AOwner: TComponent); override;
    procedure SetColor (OBrush: TColor); virtual;
  published
    { Published-Deklarationen }
    property OnClick;
    property OnMouseDown;
    property OnMouseUp;
    property OnEnter;
    property OnExit;
    property OnKeyPress;
    property OnKeyDown;
    property OnKeyUp;
    property Caption: String read FCaption write SetCaption;
    property Color: TColor read FColor write SetColor;
  end;

procedure Register;


implementation

constructor TOButton.Create (AOwner: TComponent);
begin
  inherited Create (AOwner);
  FColor := clBtnFace;
  Canvas.Brush.Color := FColor;
  Pressed := false;
  Caption := 'OButton';
  SetBounds (0,0,50,50);
end;

procedure TOButton.SetCaption (OText: String);
begin
  FCaption := OText;
  Repaint;
end;

procedure TOButton.SetColor (OBrush: TColor);
begin
  FColor := OBrush;
  Canvas.Brush.Color := FColor;
  Repaint;
end;

procedure TOButton.MouseDown (Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  inherited MouseDown (Button, Shift, X, Y);
  if Button = mbLeft then
  begin
    Pressed := true;
    Paint;
  end;
end;

procedure TOButton.MouseUp (Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  inherited MouseUp (Button, Shift, X, Y);
  Pressed := false;
  Paint;
end;

procedure TOButton.Paint;
var i: Integer;
begin
  // Position für Anzeige von Caption
  x := (Width - Canvas.TextWidth(Caption)) div 2;
  y := (Height- Canvas.TextHeight(Caption)) div 2;
  // OButton gedrückt
  if Pressed then
    for i := 1 to 3 do
    begin
      // Canvas.Pen.Color := OColor[4-i];
      Canvas.Pen.Color := OColor[(i+1) div 2];
      Canvas.Arc (i, i, Width-i, Height-i, i, i, i, i);
      Canvas.Ellipse (3, 3, Width-3, Height-3);
      Canvas.TextOut (x+1,y+1, Caption+' ');
    end
  // OButton nicht gedrückt
  else
    for i := 1 to 3 do
    begin
      Canvas.Pen.Color := OColor[i];
      Canvas.Arc (i, i, Width-i, Height-i, i, i, i, i);
      Canvas.Ellipse (3, 3, Width-3, Height-3);
      Canvas.TextOut (x,y, Caption+' ');
    end;
end;

procedure Register;
begin
  RegisterComponents ('Zusätzlich', [TOButton]);
end;

end.
