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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
| type
TForm1 = class(TForm)
SBBrush: TSpeedButton;
SBColor: TSpeedButton;
MainMenu1: TMainMenu;
N1: TMenuItem;
MOpen: TMenuItem;
MSave: TMenuItem;
MPravka: TMenuItem;
Undo: TMenuItem;
MExit: TMenuItem;
OpenPictureDialog1: TOpenPictureDialog;
Image1: TImage;
Image2: TImage;
Image3: TImage;
Label1: TLabel;
Label2: TLabel;
Image4: TImage;
SBPen: TSpeedButton;
SBRect: TSpeedButton;
SBRectang: TSpeedButton;
SBFillRec: TSpeedButton;
SBErase: TSpeedButton;
SBLine: TSpeedButton;
SavePictureDialog1: TSavePictureDialog;
MCopy: TMenuItem;
MPaste: TMenuItem;
MAbout: TMenuItem;
MCut: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure MOpenClick(Sender: TObject);
procedure UndoClick(Sender: TObject);
procedure SBBrushClick(Sender: TObject);
procedure Image3MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image3MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Image3MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure MCopyClick(Sender: TObject);
procedure MPasteClick(Sender: TObject);
procedure MSaveClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
BitMap, BMCopy:TBitMap;
R, R0:TRect;
X0,Y0,X1,Y1:longint;
RBegin, REnd, RDrag, MCut:boolean;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
HW,I:integer;
begin
BitMap:=TBitMap.Create;
Image1.Canvas.Brush.Color:=clBlack;
Image2.Canvas.Brush.Color:=clWhite;
with Image1.Canvas do FillRect(Rect(0,0,Width,Height));
with Image2.Canvas do FillRect(Rect(0,0,Width,Height));
HW:=Image4.Width div 10;
with Image4.Canvas do
begin
for I:=1 to 10 do
case I of
1: Brush.Color:=clBlack;
2: Brush.Color:=clAqua;
3: Brush.Color:=clBlue;
4: Brush.Color:=clFuchsia;
5: Brush.Color:=clGreen;
6: Brush.Color:=clLime;
7: Brush.Color:=clMaroon;
8: Brush.Color:=clRed;
9: Brush.Color:=clYellow;
10: Brush.Color:=clWhite;
end;
Rectangle((I-1)*HW,0,I*HW,Height);
end;
with Image3 do
begin
Canvas.MoveTo(0,0);
Canvas.LineTo(Width,Height);
Canvas.MoveTo(0,Height);
Canvas.LineTo(Width,0);
end;
BitMap.Assign(Image3.Picture);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
BitMap.Free;
end;
procedure TForm1.MOpenClick(Sender: TObject);
begin
if OpenPictureDialog1.Execute then
begin
Image3.Picture.LoadFromFile(OpenPictureDialog1.FileName);
BitMap.Assign(Image3.Picture);
end;
end;
procedure TForm1.UndoClick(Sender: TObject);
begin
Image3.Picture.Assign(BitMap);
end;
procedure TForm1.SBBrushClick(Sender: TObject);
begin
if (Sender as TSpeedButton).Down then
BitMap.Assign(Image3.Picture);
RBegin := false;
RDrag := false;
REnd := false;
end;
procedure TForm1.Image3MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if (Sender = Image4) or SBColor.Down then
begin
if (Button = mbLeft) then
with Image1.Canvas do
begin
Brush.Color:=(Sender as TImage).Canvas.Pixels[X,Y];
FillRect(rect(0,0,Width,Height));
end
else
with Image2.Canvas do
begin
Brush.Color:=(Sender as TImage).Canvas.Pixels[X,Y];
FillRect(Rect(0,0,Width,Height));
end;
end
else
With Image3.Canvas do
begin
X0:=x;
Y0:=Y;
if SBpen.Down then
begin
MoveTo(X,Y);
Pen.Color:=Image1.Canvas.Brush.Color;
end
else if SBLine.Down then
begin
X1:=X;
Y1:=Y;
Pen.Mode:=pmNotXor;
Pen.Color:=Image1.Canvas.Brush.Color;
end
else if SBBrush.Down then
begin
if
Button = mbLeft then
Brush.Color:=Image1.Canvas.Brush.Color
else
Brush.Color:=Image2.Canvas.Brush.Color;
FloodFill(X,Y,Pixels[X,Y],fsSurface);
end
else if SBErase.Down then
begin
R:=Rect(X-6,Y-6,X+6,Y+6);
DrawFocusRect(R);
Brush.Color:=Image2.Canvas.Brush.Color;
FillRect(Rect(X-5,Y-5,X+5,Y+5));
end
else if SBRect.Down or SBrectang.Down or SBFillRec.Down then
begin
if REnd then
begin
DrawFocusRect(R);
if (x>R.Left) and (X<R.Right) and (Y>R.Top) and (Y<R.Bottom) then
begin
RDrag:=true;
REnd:=false;
R0.TopLeft:=R.TopLeft;
R0.BottomRight:=R.BottomRight;
BitMap.Assign(Image3.Picture);
Brush.Color:=Image2.Canvas.Brush.Color;
MCopy.Enabled:=false;
MCut.Enabled:=false;
end;
end
else
begin
RBegin:=true;
REnd:=false;
R.TopLeft:=Point(X,Y);
R.BottomRight:=Point(X,Y);
DrawFocusRect(R);
end;
end;
end;
end;
procedure TForm1.Image3MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if not (ssLeft in Shift) then
exit;
if SBLine.Down then
with Image3.Canvas do
begin
MoveTo(X0,Y0);
LineTo(X1,Y1);
MoveTo(X0,Y0);
LineTo(X,Y);
X1:=X;
Y1:=Y;
end
else if SBPen.Down then
Image3.Canvas.LineTo(X, Y)
else if SBErase.Down then
with Image3.Canvas do
begin
DrawFocusRect(R);
R := Rect(X - 6, Y - 6, X + 6, Y + 6);
DrawFocusRect(R);
FillRect(Rect(X - 5, Y - 5, X + 5, Y + 5));
end
else if (SBRect.Down and (RBegin or RDrag)) or
SBRectang.Down or SBFillRec.Down then
with Image3.Canvas do
begin
if RBegin then
begin
DrawFocusRect(R);
if X0 < X then
begin
R.Left := X0;
R.Right := X
end
else
begin
R.Left := X;
R.Right := X0
end;
if Y0 < Y then
begin
R.Top := Y0;
R.Bottom := Y
end
else
begin
R.Top := Y;
R.Bottom := Y0
end;
DrawFocusRect(R);
end
else if SBRect.Down then
begin
CopyRect(R, BitMap.Canvas, R);
if not (ssCtrl in Shift) then FillRect(R0);
R.Left := R.Left + X - X0;
R.Right := R.Right + X - X0;
R.Top := R.Top + Y - Y0;
R.Bottom := R.Bottom + Y - Y0;
X0 := X;
Y0 := Y;
CopyRect(R, BitMap.Canvas, R0);
DrawFocusRect(R);
end;
end;
end;
procedure TForm1.Image3MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
with Image3.Canvas do
begin
if SBLine.Down then
begin
MoveTo(X0, Y0);
LineTo(X1, Y1);
Pen.Mode := pmCopy;
MoveTo(X0, Y0);
LineTo(X, Y);
end
else if SBRect.Down then
begin
if RDrag then
DrawFocusRect(R);
if RBegin and not REnd then
begin
REnd := true;
MCopy.Enabled := true;
MCut.Enabled := true;
end
end
else if SBRectang.Down then
begin
Brush.Color := Image1.Canvas.Brush.Color;
FrameRect(R);
end
else if SBFillRec.Down then
begin
Brush.Color := Image2.Canvas.Brush.Color;
Pen.Color := Image1.Canvas.Brush.Color;
Rectangle(R.Left, R.Top, R.Right, R.Bottom);
end
else if
SBErase.Down then
Image3.Canvas.DrawFocusRect(R);
RBegin := false;
RDrag := false;
end;
end;
procedure TForm1.MCopyClick(Sender: TObject);
var
MyFormat: Word;
AData: THandle;
APalette: HPALETTE;
begin
Image3.Canvas.DrawFocusRect(R);
BMCopy := BitMap.Create;
BMCopy.Width := R.Right - R.Left;
BMCopy.Height := R.Bottom - R.Top;
try
BMCopy.Canvas.Copyrect(Rect(0, 0, BMCopy.Width, BMCopy.Height), Image3.Canvas, R);
Image3.Canvas.DrawFocusRect(R);
BMCopy.SaveToClipBoardFormat(MyFormat,AData,APalette);
//ClipBoard.SetAsHandle(MyFormat,AData);
//ClipBoard.Assign(BMCopy);
if (Sender as TMenuItem).Name = 'MCut' then
begin
Image3.Canvas.Brush.Color := clWhite;
Image3.Canvas.FillRect(R);
end;
finally
BMCopy.Free;
end;
end;
procedure TForm1.MPasteClick(Sender: TObject);
begin
BMCopy := BitMap.Create;
try
try
// BMCopy.LoadFromClipBoardFormat(cf_BitMap,
//ClipBoard.GetAsHandle(cf_Bitmap), 0);
// Image3.Canvas.CopyRect(Rect(0, 0, BMCopy.Width, BMCopy.Height),
// BMCopy.Canvas, Rect(0, 0, BMCopy.Width, BMCopy.Height));
finally
BMCopy.Free;
end;
except
on EInvalidGraphic do
ShowMessage('Ошибочный формат графики');
end;
end;
procedure TForm1.MSaveClick(Sender: TObject);
begin
if SavePictureDialog1.Execute then
begin
BitMap.Assign(Image3.Picture);
BitMap.SaveToFile(SavePictureDialog1.FileName);
end;
end;
end. |