1 | unit CtDraw;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
|
---|
7 | ExtCtrls, StdCtrls, ExtDlgs, CoolTrayIcon;
|
---|
8 |
|
---|
9 | type
|
---|
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 |
|
---|
46 | var
|
---|
47 | DrawForm: TDrawForm;
|
---|
48 |
|
---|
49 | implementation
|
---|
50 |
|
---|
51 | {$R *.DFM}
|
---|
52 |
|
---|
53 | procedure TDrawForm.FormCreate(Sender: TObject);
|
---|
54 | begin
|
---|
55 | DrawBitmap := TBitmap.Create;
|
---|
56 | DrawBitmap.Width := 16;
|
---|
57 | DrawBitmap.Height := 16;
|
---|
58 | // Clear;
|
---|
59 | end;
|
---|
60 |
|
---|
61 |
|
---|
62 | procedure TDrawForm.FormClose(Sender: TObject; var Action: TCloseAction);
|
---|
63 | begin
|
---|
64 | DrawBitmap.Free;
|
---|
65 | end;
|
---|
66 |
|
---|
67 |
|
---|
68 | procedure TDrawForm.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
|
---|
69 | Shift: TShiftState; X, Y: Integer);
|
---|
70 | begin
|
---|
71 | StartX := X;
|
---|
72 | StartY := Y;
|
---|
73 | Drawing := True;
|
---|
74 | end;
|
---|
75 |
|
---|
76 |
|
---|
77 | procedure TDrawForm.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
|
---|
78 | Shift: TShiftState; X, Y: Integer);
|
---|
79 | begin
|
---|
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;
|
---|
88 | end;
|
---|
89 |
|
---|
90 |
|
---|
91 | procedure TDrawForm.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState;
|
---|
92 | X, Y: Integer);
|
---|
93 | begin
|
---|
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;
|
---|
111 | end;
|
---|
112 |
|
---|
113 |
|
---|
114 | procedure TDrawForm.Clear;
|
---|
115 | begin
|
---|
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;
|
---|
123 | end;
|
---|
124 |
|
---|
125 |
|
---|
126 | procedure TDrawForm.SetSolidPen;
|
---|
127 | begin
|
---|
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;
|
---|
135 | end;
|
---|
136 |
|
---|
137 |
|
---|
138 | procedure TDrawForm.SetFeatherPen;
|
---|
139 | begin
|
---|
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;
|
---|
147 | end;
|
---|
148 |
|
---|
149 |
|
---|
150 | procedure TDrawForm.CopyToBitmap(const Bmp: TBitmap);
|
---|
151 | var
|
---|
152 | DrawCanvas: TCanvas;
|
---|
153 | begin
|
---|
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;
|
---|
165 | end;
|
---|
166 |
|
---|
167 |
|
---|
168 | procedure TDrawForm.UpdateIcon;
|
---|
169 | var
|
---|
170 | Ico: TIcon;
|
---|
171 | MaskColor: TColor;
|
---|
172 | begin
|
---|
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;
|
---|
191 | end;
|
---|
192 |
|
---|
193 |
|
---|
194 | procedure TDrawForm.Button1Click(Sender: TObject);
|
---|
195 | begin
|
---|
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);
|
---|
201 | end;
|
---|
202 |
|
---|
203 |
|
---|
204 | procedure TDrawForm.Button2Click(Sender: TObject);
|
---|
205 | begin
|
---|
206 | Clear;
|
---|
207 | end;
|
---|
208 |
|
---|
209 |
|
---|
210 | procedure TDrawForm.Button3Click(Sender: TObject);
|
---|
211 | var
|
---|
212 | Bmp: TBitmap;
|
---|
213 | begin
|
---|
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;
|
---|
226 | end;
|
---|
227 |
|
---|
228 |
|
---|
229 | procedure TDrawForm.Button4Click(Sender: TObject);
|
---|
230 | begin
|
---|
231 | Close;
|
---|
232 | end;
|
---|
233 |
|
---|
234 |
|
---|
235 | procedure TDrawForm.PaintBox1Paint(Sender: TObject);
|
---|
236 | begin
|
---|
237 | with PaintBox1 do
|
---|
238 | Canvas.CopyRect(ClientRect, DrawBitmap.Canvas, ClientRect);
|
---|
239 | end;
|
---|
240 |
|
---|
241 |
|
---|
242 | procedure TDrawForm.CheckBox1Click(Sender: TObject);
|
---|
243 | begin
|
---|
244 | UpdateIcon;
|
---|
245 | end;
|
---|
246 |
|
---|
247 | end.
|
---|
248 |
|
---|