-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBalloon.pas
394 lines (315 loc) · 11.3 KB
/
Balloon.pas
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
{
Balloon - using Balloon-shaped windows in your Delphi programs
Copyright (C) 2003 JWB Software
Web: http://people.zeelandnet.nl/famboek/delphi/
Email: [email protected]
}
Unit Balloon;
Interface
Uses
Forms, Classes, Controls, StdCtrls, ExtCtrls, Windows, Graphics,
Messages, SysUtils;
Type
TBalloonType = (blnInfo, blnError, blnWarning);
TBalloonHoriz = (blnLeft, blnMiddle, blnRight);
TBalloonVert = (blnTop, blnCenter, blnBottom);
TBalloonPosition = (blnArrowTopLeft, blnArrowTopRight, blnArrowBottomLeft, blnArrowBottomRight);
Type
TBalloonControl = Class(TComponent)
private
FTitle: String;
FText: TStringList;
FDuration, FPixelCoordinateX, FPixelCoordinateY: Integer;
FHorizontal: TBalloonHoriz;
FVertical: TBalloonVert;
FPosition: TBalloonPosition;
FControl: TWinControl;
FBalloonType: TBalloonType;
procedure SetText(Value: TStringList);
public
procedure ShowControlBalloon;
procedure ShowPixelBalloon;
published
property Text: TStringList read FText write SetText;
property Title: String read FTitle write FTitle;
property Duration: Integer read FDuration write FDuration;
property Horizontal: TBalloonHoriz read FHorizontal write FHorizontal;
property Vertical: TBalloonVert read FVertical write FVertical;
property Position: TBalloonPosition read FPosition write FPosition;
property Control: TWinControl read FControl write FControl;
property PixelCoordinateX: Integer read FPixelCoordinateX write FPixelCoordinateX;
property PixelCoordinateY: Integer read FPixelCoordinateY write FPixelCoordinateY;
property BalloonType: TBalloonType read FBalloonType write FBalloonType;
Constructor Create(AOwner: TComponent); override;
Destructor Destroy; override;
End;
Type
TBalloon = Class(TCustomForm)
private
lblTitle: TLabel;
lblText: TLabel;
pnlAlign: TPanel;
iconBitmap: TImage;
tmrExit: TTimer;
Procedure FormPaint(Sender: TObject);
protected
Procedure CreateParams(Var Params: TCreateParams); override;
Procedure OnMouseClick(Sender: TObject);
Procedure OnExitTimer(Sender: TObject);
Procedure OnChange(Sender: TObject);
Procedure WndProc(Var Message: TMessage); override;
public
Constructor CreateNew(AOwner: TComponent; Dummy: Integer = 0); override;
Procedure ShowBalloon(blnLeft, blnTop: Integer; blnTitle, blnText: String; blnType: TBalloonType; blnDuration: Integer; blnPosition: TBalloonPosition);
Procedure ShowControlBalloon(blnControl: TWinControl; blnHoriz: TBalloonHoriz; blnVert: TBalloonVert; blnTitle, blnText: String; blnType: TBalloonType; blnDuration: Integer);
End;
Procedure Register;
Implementation
{$R Balloon.res}
Procedure Register;
Begin
RegisterComponents('Custom', [TBalloonControl]);
End;
Constructor TBalloonControl.Create(AOwner: TComponent);
Begin
Inherited;
FText := TStringList.Create;
End;
Destructor TBalloonControl.Destroy;
Begin
FText.Free;
Inherited;
End;
procedure TBalloonControl.SetText(Value: TStringList);
begin
FText.Assign(Value);
end;
Procedure TBalloonControl.ShowControlBalloon();
Var
Balloon: TBalloon;
Begin
Balloon := TBalloon.CreateNew(Owner);
Balloon.ShowControlBalloon(FControl, FHorizontal, FVertical, FTitle, Trim(FText.Text), FBalloonType, FDuration);
End;
Procedure TBalloonControl.ShowPixelBalloon();
Var
Balloon: TBalloon;
Begin
Balloon := TBalloon.CreateNew(nil);
Balloon.ShowBalloon(FPixelCoordinateX, FPixelCoordinateY, FTitle, Trim(FText.Text), FBalloonType, FDuration, FPosition);
End;
Procedure TBalloon.CreateParams(Var Params: TCreateParams);
Begin
Inherited CreateParams(Params);
Params.Style := (Params.Style and not WS_CAPTION) or WS_POPUP;
Params.ExStyle := Params.ExStyle or WS_EX_TOOLWINDOW or WS_EX_NOACTIVATE or WS_EX_TOPMOST;
Params.WndParent := GetDesktopWindow;
End;
Procedure TBalloon.OnMouseClick(Sender: TObject);
Begin
Release;
End;
Procedure TBalloon.OnExitTimer(Sender: TObject);
Begin
Release;
End;
Procedure TBalloon.OnChange(Sender: TObject);
Begin
SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE);
End;
Procedure TBalloon.WndProc(Var Message: TMessage);
Begin
If (Message.Msg = WM_SIZE) and (Message.WParam = SIZE_MINIMIZED) Then
Show;
Inherited;
End;
Constructor TBalloon.CreateNew(AOwner: TComponent; Dummy: Integer = 0);
Begin
Inherited;
OnActivate := OnChange;
OnDeactivate := OnChange;
OnShow := OnChange;
BorderStyle := bsNone;
FormStyle := fsStayOnTop;
OnPaint := FormPaint;
Color := clInfoBk;
Font.Name := 'Tahoma';
pnlAlign := TPanel.Create(Self);
lblTitle := TLabel.Create(Self);
lblText := TLabel.Create(Self);
iconBitmap := TImage.Create(Self);
tmrExit := TTimer.Create(Self);
OnClick := OnMouseClick;
iconBitmap.OnClick := OnMouseClick;
pnlAlign.OnClick := OnMouseClick;
lblTitle.OnClick := OnMouseClick;
lblText.OnClick := OnMouseClick;
lblTitle.Parent := Self;
lblTitle.ParentColor := True;
lblTitle.ParentFont := True;
lblTitle.AutoSize := True;
lblTitle.Font.Style := [fsBold];
lblTitle.Left := 34;
lblTitle.Top := 12;
lblText.Parent := Self;
lblText.ParentColor := True;
lblText.ParentFont := True;
lblText.AutoSize := True;
lblText.Left := 10;
iconBitmap.Parent := Self;
iconBitmap.Transparent := True;
iconBitmap.Left := 10;
iconBitmap.Top := 10;
tmrExit.Enabled := False;
tmrExit.Interval := 0;
tmrExit.OnTimer := OnExitTimer;
End;
Procedure TBalloon.FormPaint(Sender: TObject);
Var
TempRegion: HRGN;
Begin
With Canvas.Brush Do
Begin
Color := clBlack;
Style := bsSolid;
End;
TempRegion := CreateRectRgn(0,0,1,1);
GetWindowRgn(Handle, TempRegion);
FrameRgn(Canvas.Handle, TempRegion, Canvas.Brush.handle, 1, 1);
DeleteObject(TempRegion);
End;
Procedure TBalloon.ShowControlBalloon(blnControl: TWinControl; blnHoriz: TBalloonHoriz; blnVert: TBalloonVert; blnTitle, blnText: String; blnType: TBalloonType; blnDuration: Integer);
Var
Rect: TRect;
blnPosLeft, blnPosTop: Integer;
blnPosition: TBalloonPosition;
Begin
GetWindowRect(blnControl.Handle, Rect);
blnPosTop := 0;
blnPosLeft := 0;
If blnVert = blnTop Then
blnPosTop := Rect.Top;
If blnVert = blnCenter Then
blnPosTop := Rect.Top + Round((Rect.Bottom - Rect.Top) / 2);
If blnVert = blnBottom Then
blnPosTop := Rect.Bottom;
If blnHoriz = blnLeft Then
blnPosLeft := Rect.Left;
If blnHoriz = blnMiddle Then
blnPosLeft := Rect.Left + Round((Rect.Right - Rect.Left) / 2);
If blnHoriz = blnRight Then
blnPosLeft := Rect.Right;
blnPosition := blnArrowBottomRight;
If ((blnHoriz = blnRight) and (blnVert = blnBottom)) or ((blnHoriz = blnMiddle) and (blnVert = blnBottom)) Then
blnPosition := blnArrowBottomRight;
If (blnHoriz = blnLeft) and (blnVert = blnBottom) or ((blnHoriz = blnLeft) and (blnVert = blnCenter)) Then
blnPosition := blnArrowBottomLeft;
If (blnHoriz = blnLeft) and (blnVert = blnTop) or ((blnHoriz = blnMiddle) and (blnVert = blnTop)) Then
blnPosition := blnArrowTopLeft;
If (blnHoriz = blnRight) and (blnVert = blnTop) or ((blnHoriz = blnRight) and (blnVert = blnCenter)) Then
blnPosition := blnArrowTopRight;
ShowBalloon(blnPosLeft, blnPosTop, blnTitle, blnText, blnType, blnDuration, blnPosition);
End;
Procedure TBalloon.ShowBalloon(blnLeft, blnTop: Integer; blnTitle, blnText: String; blnType: TBalloonType; blnDuration: Integer; blnPosition: TBalloonPosition);
Var
ArrowHeight, ArrowWidth: Integer;
FormRegion, ArrowRegion: HRGN;
Arrow: Array [0..2] Of TPoint;
ResName: String;
Begin
ArrowHeight := 20;
ArrowWidth := 20;
lblTitle.Caption := blnTitle;
If blnPosition = blnArrowBottomRight Then
lblTitle.Top := lblTitle.Top + ArrowHeight;
If blnPosition = blnArrowBottomLeft Then
lblTitle.Top := lblTitle.Top + ArrowHeight;
lblText.Top := lblTitle.Top + lblTitle.Height + 8;
lblText.Caption := blnText;
If blnPosition = blnArrowBottomRight Then
iconBitmap.Top := iconBitmap.Top + ArrowHeight;
If blnPosition = blnArrowBottomLeft Then
iconBitmap.Top := iconBitmap.Top + ArrowHeight;
Case blnType Of
blnError:
ResName := 'ERROR';
blnInfo:
ResName := 'INFO';
blnWarning:
ResName := 'WARNING';
Else
ResName := 'INFO';
End;
iconBitmap.Picture.Bitmap.LoadFromResourceName(HInstance, ResName);
If blnPosition = blnArrowBottomRight Then
ClientHeight := lblText.Top + lblText.Height + 10;
If blnPosition = blnArrowBottomLeft Then
ClientHeight := lblText.Top + lblText.Height + 10;
If blnPosition = blnArrowTopLeft Then
ClientHeight := lblText.Top + lblText.Height + 10 + ArrowHeight;
If blnPosition = blnArrowTopRight Then
ClientHeight := lblText.Top + lblText.Height + 10 + ArrowHeight;
If (lblTitle.Left + lblTitle.Width) > (lblText.Left + lblText.Width) Then
Width := lblTitle.Left + lblTitle.Width + 10
Else
Width := lblText.Left + lblText.Width + 10;
If blnPosition = blnArrowTopLeft Then
Begin
Left := blnLeft - (Width - 20);
Top := blnTop - (Height);
End;
If blnPosition = blnArrowTopRight Then
Begin
Left := blnLeft - 20;
Top := blnTop - (Height);
End;
If blnPosition = blnArrowBottomRight Then
Begin
Left := blnLeft - 20;
Top := blnTop - 2;
End;
If blnPosition = blnArrowBottomLeft Then
Begin
Left := blnLeft - (Width - 20);
Top := blnTop - 2;
End;
FormRegion := 0;
If blnPosition = blnArrowTopLeft Then
Begin
FormRegion := CreateRoundRectRgn(0, 0, Width, Height - (ArrowHeight - 2), 7, 7);
Arrow[0] := Point(Width - ArrowWidth - 20, Height - ArrowHeight);
Arrow[1] := Point(Width - 20, Height);
Arrow[2] := Point(Width - 20, Height - ArrowHeight);
End;
If blnPosition = blnArrowTopRight Then
Begin
FormRegion := CreateRoundRectRgn(0, 0, Width, Height - (ArrowHeight - 2), 7, 7);
Arrow[0] := Point(20, Height - ArrowHeight);
Arrow[1] := Point(20, Height);
Arrow[2] := Point(20 + ArrowWidth, Height - ArrowHeight);
End;
If blnPosition = blnArrowBottomRight Then
Begin
FormRegion := CreateRoundRectRgn(0, ArrowHeight + 2, Width, Height, 7, 7);
Arrow[0] := Point(20, 2);
Arrow[1] := Point(20, ArrowHeight + 2);
Arrow[2] := Point(20 + ArrowWidth, ArrowHeight + 2);
End;
If blnPosition = blnArrowBottomLeft Then
Begin
FormRegion := CreateRoundRectRgn(0, ArrowHeight + 2, Width, Height, 7, 7);
Arrow[0] := Point(Width - 20, 2);
Arrow[1] := Point(Width - 20, ArrowHeight + 2);
Arrow[2] := Point(Width - 20 - ArrowWidth, ArrowHeight + 2);
End;
ArrowRegion := CreatePolygonRgn(Arrow, 3, WINDING);
CombineRgn(FormRegion, FormRegion, ArrowRegion, RGN_OR);
DeleteObject(ArrowRegion);
SetWindowRgn(Handle, FormRegion, True);
Visible := False;
ShowWindow(Handle, SW_SHOWNOACTIVATE);
Visible := True;
tmrExit.Interval := blnDuration * 1000;
tmrExit.Enabled := True;
End;
End.