1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
| unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TShape = class (ExtCtrls.TShape)
public
property Canvas;
procedure Paint; override;
constructor Create(aowner:TComponent); override;
destructor Destroy; override;
procedure ResetCanvas;
Procedure SaveCanvas;
protected
savePaint: boolean;
CanvasBufer: Tbitmap;
end;
TForm1 = class(TForm)
Shape1: TShape;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
// чтото рисуем
Shape1.Canvas.Pen.Color:=clRed;
Shape1.Canvas.Brush.Color:=clBlue;
Shape1.Canvas.MoveTo(20,20);
Shape1.Canvas.LineTo(100,100);
Shape1.Canvas.MoveTo(40,20);
Shape1.Canvas.LineTo(120,100);
// сохраняем изменения
Shape1.SaveCanvas;
end;
{ TShape }
constructor TShape.Create(aowner: TComponent);
begin
inherited;
CanvasBufer:=Tbitmap.Create;
savePaint:=false;
end;
destructor TShape.Destroy;
begin
CanvasBufer.Free;
inherited;
end;
procedure TShape.Paint;
var r:TRect;
begin
if savePaint then begin
r:=Bounds(0,0,self.Width,self.Height);
Canvas.CopyRect(r,CanvasBufer.Canvas,r);
end else
inherited;
end;
procedure TShape.ResetCanvas;
begin
savePaint:=false;
Repaint;
end;
procedure TShape.SaveCanvas;
var r:TRect;
begin
r:=Bounds(0,0,self.Width,self.Height);
CanvasBufer.Width:=Width;
CanvasBufer.Height:=Height;
CanvasBufer.Canvas.CopyRect(r,Canvas,r);
savePaint:=true;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
// сброс канваса
Shape1.ResetCanvas;
end;
end. |