source: components/CoolTrayIcon/demos/TrayDraw/CtDraw.pas

Last change on this file was 1, checked in by maron, 16 years ago

3.1 verze, první revize

File size: 5.5 KB
Line 
1unit CtDraw;
2
3interface
4
5uses
6 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7 ExtCtrls, StdCtrls, ExtDlgs, CoolTrayIcon;
8
9type
10 TDrawForm = class(TForm)
11 PaintBox1: TPaintBox;
12 Button2: TButton;
13 CoolTrayIcon1: TCoolTrayIcon;
14 Button3: TButton;
15 Button4: TButton;
16 OpenPictureDialog1: TOpenPictureDialog;
17 Shape1: TShape;
18 CheckBox1: TCheckBox;
19 Button1: TButton;
20 procedure FormCreate(Sender: TObject);
21 procedure FormClose(Sender: TObject; var Action: TCloseAction);
22 procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
23 Shift: TShiftState; X, Y: Integer);
24 procedure PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
25 Shift: TShiftState; X, Y: Integer);
26 procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState;
27 X, Y: Integer);
28 procedure Button1Click(Sender: TObject);
29 procedure Button2Click(Sender: TObject);
30 procedure Button3Click(Sender: TObject);
31 procedure Button4Click(Sender: TObject);
32 procedure PaintBox1Paint(Sender: TObject);
33 procedure CheckBox1Click(Sender: TObject);
34 private
35 StartX, StartY: Integer;
36 CurrentX, CurrentY: Integer;
37 Drawing: Boolean;
38 DrawBitmap: TBitmap;
39 procedure UpdateIcon;
40 procedure CopyToBitmap(const Bmp: TBitmap);
41 procedure SetSolidPen;
42 procedure SetFeatherPen;
43 procedure Clear;
44 end;
45
46var
47 DrawForm: TDrawForm;
48
49implementation
50
51{$R *.DFM}
52
53procedure TDrawForm.FormCreate(Sender: TObject);
54begin
55 DrawBitmap := TBitmap.Create;
56 DrawBitmap.Width := 16;
57 DrawBitmap.Height := 16;
58// Clear;
59end;
60
61
62procedure TDrawForm.FormClose(Sender: TObject; var Action: TCloseAction);
63begin
64 DrawBitmap.Free;
65end;
66
67
68procedure TDrawForm.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
69 Shift: TShiftState; X, Y: Integer);
70begin
71 StartX := X;
72 StartY := Y;
73 Drawing := True;
74end;
75
76
77procedure TDrawForm.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
78 Shift: TShiftState; X, Y: Integer);
79begin
80 SetSolidPen;
81 PaintBox1.Canvas.MoveTo(StartX, StartY);
82 PaintBox1.Canvas.LineTo(X, Y);
83 CurrentX := 0;
84 CurrentY := 0;
85 Drawing := False;
86 CopyToBitmap(DrawBitmap);
87 UpdateIcon;
88end;
89
90
91procedure TDrawForm.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState;
92 X, Y: Integer);
93begin
94 if Drawing then
95 with PaintBox1.Canvas do
96 begin
97 // Erase old line
98 if (CurrentX <> 0) and (CurrentY <> 0) then
99 begin
100 SetFeatherPen;
101 MoveTo(StartX, StartY);
102 LineTo(CurrentX, CurrentY);
103 end;
104 // Draw new line
105 SetFeatherPen;
106 MoveTo(StartX, StartY);
107 LineTo(X, Y);
108 CurrentX := X;
109 CurrentY := Y;
110 end;
111end;
112
113
114procedure TDrawForm.Clear;
115begin
116 PaintBox1.Canvas.Brush.Color := clWhite;
117 PaintBox1.Canvas.FillRect(Rect(0, 0, PaintBox1.Width, PaintBox1.Height));
118 if not DrawBitmap.Empty then
119 begin
120 CopyToBitmap(DrawBitmap);
121 UpdateIcon;
122 end;
123end;
124
125
126procedure TDrawForm.SetSolidPen;
127begin
128 with PaintBox1.Canvas do
129 begin
130 Pen.Mode := pmCopy;
131 Pen.Style := psSolid;
132 Pen.Width := 8;
133 Pen.Color := clBlack;
134 end;
135end;
136
137
138procedure TDrawForm.SetFeatherPen;
139begin
140 with PaintBox1.Canvas do
141 begin
142 Pen.Mode := pmNotXor;
143 Pen.Style := psDot;
144 Pen.Width := 1;
145 Pen.Color := clBlack;
146 end;
147end;
148
149
150procedure TDrawForm.CopyToBitmap(const Bmp: TBitmap);
151var
152 DrawCanvas: TCanvas;
153begin
154 DrawCanvas := TCanvas.Create;
155 try
156 DrawCanvas.Handle := PaintBox1.Canvas.Handle;
157 Bmp.Width := PaintBox1.Width;
158 Bmp.Height := PaintBox1.Height;
159 Bmp.Canvas.CopyRect(PaintBox1.ClientRect, DrawCanvas, PaintBox1.ClientRect);
160 // Resize to 16x16
161 Bmp.Canvas.StretchDraw(Rect(0, 0, 16, 16), Bmp);
162 finally
163 DrawCanvas.Free;
164 end;
165end;
166
167
168procedure TDrawForm.UpdateIcon;
169var
170 Ico: TIcon;
171 MaskColor: TColor;
172begin
173 Ico := TIcon.Create;
174 try
175 if CheckBox1.Checked then
176 // Find transparent color (bottom left pixel)
177 MaskColor := DrawBitmap.Canvas.Pixels[0, DrawBitmap.Height-1]
178 else
179 // Not transparent
180 MaskColor := clNone;
181
182 if CoolTrayIcon1.BitmapToIcon(DrawBitmap, Ico, MaskColor) then
183 begin
184 // OK, let's assign the icon
185 CoolTrayIcon1.Icon.Assign(Ico);
186 CoolTrayIcon1.Refresh;
187 end;
188 finally
189 Ico.Free;
190 end;
191end;
192
193
194procedure TDrawForm.Button1Click(Sender: TObject);
195begin
196 MessageDlg('This is just a silly demo of how CoolTrayIcon can render ' +
197 'a tray icon from a bitmap.' + #13#13 +
198 'Use the mouse to draw some lines or load a bitmap. ' +
199 'Watch how the tray icon changes.',
200 mtInformation, [mbOK], 0);
201end;
202
203
204procedure TDrawForm.Button2Click(Sender: TObject);
205begin
206 Clear;
207end;
208
209
210procedure TDrawForm.Button3Click(Sender: TObject);
211var
212 Bmp: TBitmap;
213begin
214 if OpenPictureDialog1.Execute then
215 begin
216 Bmp := TBitmap.Create;
217 try
218 Bmp.LoadFromFile(OpenPictureDialog1.Filename);
219 PaintBox1.Canvas.StretchDraw(PaintBox1.ClientRect, Bmp);
220 CopyToBitmap(DrawBitmap);
221 UpdateIcon;
222 finally
223 Bmp.Free;
224 end;
225 end;
226end;
227
228
229procedure TDrawForm.Button4Click(Sender: TObject);
230begin
231 Close;
232end;
233
234
235procedure TDrawForm.PaintBox1Paint(Sender: TObject);
236begin
237 with PaintBox1 do
238 Canvas.CopyRect(ClientRect, DrawBitmap.Canvas, ClientRect);
239end;
240
241
242procedure TDrawForm.CheckBox1Click(Sender: TObject);
243begin
244 UpdateIcon;
245end;
246
247end.
248
Note: See TracBrowser for help on using the repository browser.