unit Forms_MainForm;
interface
uses
PaintStrategy,
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.ExtCtrls,
Vcl.StdCtrls;
type
TForm1 =
class( TForm )
PaintBox1: TPaintBox;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Panel1: TPanel;
procedure PaintBox1MouseDown( Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer );
procedure PaintBox1MouseMove( Sender: TObject; Shift: TShiftState; X, Y: Integer );
procedure PaintBox1MouseUp( Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer );
procedure PaintBox1Paint( Sender: TObject );
procedure Button1Click( Sender: TObject );
procedure Button2Click( Sender: TObject );
procedure Button3Click( Sender: TObject );
procedure Button4Click( Sender: TObject );
private
FNullPaintStrategy : TPaintStrategy;
FCurrentPaintStrategy: TPaintStrategy;
procedure SetCurrentPaintStrategy(
const Value: TPaintStrategy );
function GetCurrentPaintStrategy: TPaintStrategy;
protected
property CurrentPaintStrategy: TPaintStrategy
read GetCurrentPaintStrategy
write SetCurrentPaintStrategy;
public
procedure AfterConstruction;
override;
procedure BeforeDestruction;
override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.AfterConstruction;
begin
inherited;
FNullPaintStrategy := TNullPaintStrategy.Create;
end;
procedure TForm1.BeforeDestruction;
begin
FNullPaintStrategy.Free;
CurrentPaintStrategy :=
nil;
inherited;
end;
procedure TForm1.Button1Click( Sender: TObject );
begin
CurrentPaintStrategy :=
nil;
end;
procedure TForm1.Button2Click( Sender: TObject );
begin
CurrentPaintStrategy := TLinePaintStrategy.Create;
end;
procedure TForm1.Button3Click( Sender: TObject );
begin
CurrentPaintStrategy := TRectanglePaintStrategy.Create;
end;
procedure TForm1.Button4Click( Sender: TObject );
begin
CurrentPaintStrategy := TEllipsePaintStrategy.Create;
end;
function TForm1.GetCurrentPaintStrategy: TPaintStrategy;
begin
if not Assigned( FCurrentPaintStrategy )
then
Result := FNullPaintStrategy
else
Result := FCurrentPaintStrategy;
end;
procedure TForm1.PaintBox1MouseDown( Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer );
begin
CurrentPaintStrategy.MouseDown( Button, Shift, X, Y );
PaintBox1.Invalidate;
end;
procedure TForm1.PaintBox1MouseMove( Sender: TObject; Shift: TShiftState; X, Y: Integer );
begin
CurrentPaintStrategy.MouseMove( Shift, X, Y );
PaintBox1.Invalidate;
end;
procedure TForm1.PaintBox1MouseUp( Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer );
begin
CurrentPaintStrategy.MouseUp( Button, Shift, X, Y );
PaintBox1.Invalidate;
end;
procedure TForm1.PaintBox1Paint( Sender: TObject );
begin
CurrentPaintStrategy.PaintOn( TPaintBox( Sender ).Canvas );
end;
procedure TForm1.SetCurrentPaintStrategy(
const Value: TPaintStrategy );
begin
if Value <> FCurrentPaintStrategy
then
begin
FreeAndNil( FCurrentPaintStrategy );
FCurrentPaintStrategy := Value;
end;
end;
end.