-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrmshape.pas
More file actions
432 lines (370 loc) · 15.8 KB
/
frmshape.pas
File metadata and controls
432 lines (370 loc) · 15.8 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
{********************************************************************}
{ TFORMSHAPE component }
{ for Delphi & C++Builder }
{ }
{ written by }
{ }
{ TMS Software }
{ enhanced by Brian Shepherd }
{ copyright © 1998-2017 }
{ Email : [email protected] }
{ Web : http://www.tmssoftware.com }
{ }
{ The source code is given as is. The author is not responsible }
{ for any possible damage done due to the use of this code. }
{ The component can be freely used in any application. The source }
{ code remains property of the author and may not be distributed }
{ freely as such. }
{********************************************************************}
unit frmshape;
{$I TMSDEFS.INC}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Types;
const
MAJ_VER = 1; // Major version nr.
MIN_VER = 3; // Minor version nr.
REL_VER = 1; // Release nr.
BLD_VER = 0; // Build nr.
// version history
// v1.0.0.0 : First release
// v1.3.0.1 : Fixed : Issue with Win64 and Windows 8
// v1.3.1.0 : New : Method ApplyShape added
type
EFormShapeError = class(Exception);
TBalloonPosition = (bpTopLeft,bpLeftTop,bpTopRight,bpRightTop,
bpBottomLeft,bpLeftBottom,bpBottomRight,bpRightBottom,bpNone);
{$IFDEF DELPHIXE2_LVL}
[ComponentPlatformsAttribute(pidWin32 or pidWin64)]
{$ENDIF}
TFormshape = class(TComponent)
private
{ Private declarations }
FReshape:boolean;
FOnPaint:TNotifyEvent;
FOnPaintAssigned:boolean;
FBalloonPosition:TBalloonPosition;
FBalloonEllips:integer;
FBalloonIndent:integer;
FBalloonColor:Tcolor;
FBorderColor:TColor;
FBorderWidth:integer;
OldWndProc:TFarProc;
NewWndProc:Pointer;
FDragBalloon:boolean;
procedure PaintForm(sender:tobject);
procedure SetBalloonIndent(avalue:integer);
procedure SetBalloonEllips(avalue:integer);
{New procedures}
procedure SetBalloonColor(avalue:TColor);
procedure SetBorderColor(avalue:TColor);
procedure SetBorderWidth(avalue:integer);
function GetVersion: string;
procedure SetVersion(const Value: string);
protected
{ Protected declarations }
function GetVersionNr: Integer;
procedure Loaded; override;
procedure HookWndProc(var Msg: TMessage);
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure ApplyShape;
published
{ Published declarations }
property Reshape: Boolean read FReshape write FReshape;
property BalloonPosition: TBalloonposition read FBalloonPosition write FBalloonPosition;
property BalloonEllips: Integer read FBalloonEllips write SetBalloonEllips;
property BalloonIndent: Integer read FBalloonIndent write SetBalloonIndent;
{New Properties}
property BalloonColor: TColor read FBalloonColor write SetBalloonColor;
property BorderColor: TColor read FBorderColor write SetBorderColor;
property BorderWidth: Integer read FBorderWidth write SetBorderWidth;
property DragBalloon: Boolean read FDragBalloon write FDragBalloon;
property Version: string read GetVersion write SetVersion;
end;
implementation
type
{$IFDEF DELPHIXE_LVL}
LInteger = LONG_PTR;
LIntParam = LPARAM;
{$ENDIF}
{$IFNDEF DELPHIXE_LVL}
LInteger = Integer;
LIntParam = Integer;
{$ENDIF}
procedure TFormshape.ApplyShape;
var
hrgn1: THandle;
hrgn2: THandle;
hrgn: THandle;
triangle:array[0..2] of tpoint;
i: Integer;
begin
with (Owner as TForm) do
begin
borderstyle := bsNone;
SetWindowRgn(handle,0,true);
Height := Height - GetSystemMetrics(SM_CYCAPTION);
Repaint;
for i := 1 to ComponentCount do
begin
if (Components[i-1] is TControl) then
if (Components[i-1] as TControl).Parent = Owner then
(Components[i-1] as TControl).Top := (Components[i-1] as TControl).Top+GetSystemMetrics(SM_CYCAPTION);
end;
hrgn := CreateRectRgn(0,0,clientrect.right,clientrect.bottom);
case BalloonPosition of
bpNone:hrgn1:=CreateRoundRectRgn(0,0,clientrect.right,clientrect.bottom,balloonellips,balloonellips);
bpTopLeft,bpTopRight:hrgn1:=CreateRoundRectRgn(0,balloonindent,clientrect.right,clientrect.bottom,balloonellips,balloonellips);
bpLeftTop,bpLeftBottom:hrgn1:=CreateRoundRectRgn(balloonindent,0,clientrect.right,clientrect.bottom,balloonellips,balloonellips);
bpRightTop,bpRightBottom:hrgn1:=CreateRoundRectRgn(0,0,clientrect.right-balloonindent,clientrect.bottom,balloonellips,balloonellips);
bpBottomLeft,bpBottomRight:hrgn1:=CreateRoundRectRgn(0,0,clientrect.right,clientrect.bottom-balloonindent,balloonellips,balloonellips);
else
hrgn1:=CreateRoundRectRgn(0,0,clientrect.right,clientrect.bottom,balloonellips,balloonellips);
end;
case BalloonPosition of
bpTopLeft:
begin
triangle[0] := Point(balloonellips,0);
triangle[1] := Point(balloonellips,balloonindent);
triangle[2] := Point(balloonellips+balloonindent,balloonindent);
end;
bpTopRight:begin
triangle[0] := Point(clientrect.right-balloonellips,0);
triangle[1] := Point(clientrect.right-balloonellips,balloonindent);
triangle[2] := Point(clientrect.right-(balloonellips+balloonindent),balloonindent);
end;
bpBottomLeft:begin
triangle[0] := Point(balloonellips,clientrect.bottom);
triangle[1] := Point(balloonellips,clientrect.bottom-balloonindent-1);
triangle[2] := Point(balloonellips+balloonindent,clientrect.bottom-balloonindent-1);
end;
bpBottomRight:begin
triangle[0] := Point(clientrect.right-balloonellips,clientrect.bottom);
triangle[1] := Point(clientrect.right-balloonellips,clientrect.bottom-balloonindent-1);
triangle[2] := Point(clientrect.right-(balloonellips+balloonindent),clientrect.bottom-balloonindent-1);
end;
bpLeftTop:begin
triangle[0] := Point(0,balloonellips);
triangle[1] := Point(balloonindent,balloonellips);
triangle[2] := Point(balloonindent,balloonellips+balloonindent);
end;
bpLeftBottom:begin
triangle[0] := Point(0,clientrect.bottom-balloonellips);
triangle[1] := Point(balloonindent,clientrect.bottom-balloonellips);
triangle[2] := Point(balloonindent,clientrect.bottom-(balloonellips+balloonindent));
end;
bpRightTop:begin
triangle[0] := Point(clientrect.right,balloonellips);
triangle[1] := Point(clientrect.right-balloonindent-1,balloonellips);
triangle[2] := Point(clientrect.right-balloonindent-1,balloonellips+balloonindent);
end;
bpRightBottom:begin
triangle[0] := Point(clientrect.right,clientrect.bottom-balloonellips);
triangle[1] := Point(clientrect.right-balloonindent-1,clientrect.bottom-balloonellips);
triangle[2] := Point(clientrect.right-balloonindent-1,clientrect.bottom-(balloonellips+balloonindent));
end;
end;
if BalloonPosition <> bpNone then
begin
hrgn2 := CreatePolygonRgn(triangle,3,WINDING);
CombineRgn(hrgn,hrgn1,hrgn2,RGN_OR);
SetWindowRgn(handle,hrgn,true);
DeleteObject(hrgn2);
end
else
SetWindowRgn(handle,hrgn1,true);
DeleteObject(hrgn);
DeleteObject(hrgn1);
end;
end;
constructor TFormshape.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
FBalloonIndent := 15;
FBalloonEllips := 20;
{new attributes}
FBalloonColor := clWhite;
FBorderColor := clBlack;
FBorderWidth :=1;
if not (Owner is TForm) then
raise EFormShapeError.Create('Control parent must be a form!');
if (csDesigning in ComponentState) then Exit;
end;
destructor TFormShape.Destroy;
begin
if not (csDesigning in ComponentState) then
begin
{ UnHook parent }
if (Owner <> nil) and Assigned(OldWndProc) then
{$IFDEF DELPHI_UNICODE}
SetWindowLongPtr((Owner as TForm).Handle, GWL_WNDPROC, LInteger(OldWndProc));
{$ENDIF}
{$IFNDEF DELPHI_UNICODE}
SetWindowLong((Owner as TForm).Handle, GWL_WNDPROC, LInteger(OldWndProc));
{$ENDIF}
if Assigned(NewWndProc) then FreeObjectInstance(NewWndProc);
end;
inherited;
end;
procedure TFormShape.SetBalloonIndent(avalue:integer);
begin
if (avalue>0) then FBalloonIndent:=avalue;
end;
procedure TFormShape.SetBalloonEllips(avalue:integer);
begin
if (avalue>0) then FBalloonEllips:=avalue;
end;
procedure TFormShape.SetBalloonColor(avalue:TColor);
begin
FBalloonColor := avalue;
end;
procedure TFormShape.SetBorderColor(avalue:TColor);
begin
FBorderColor := avalue;
end;
procedure TFormShape.SetBorderWidth(avalue:integer);
begin
if (avalue >0) then FBorderWidth := avalue;
end;
{---------------------------------------------}
procedure TFormShape.Loaded;
begin
inherited Loaded;
if not freshape then Exit;
if (csDesigning in ComponentState) then Exit;
with (Owner as TForm) do
begin
FOnPaintAssigned:=false;
if (assigned(OnPaint)) then
begin
FOnPaint := OnPaint;
FOnPaintAssigned := true;
end;
OnPaint := PaintForm;
end;
ApplyShape;
{ Hook parent }
{$IFDEF DELPHI_UNICODE}
OldWndProc := TFarProc(GetWindowLongPtr((Owner as TForm).Handle, GWL_WNDPROC));
{$ENDIF}
{$IFNDEF DELPHI_UNICODE}
OldWndProc := TFarProc(GetWindowLong((Owner as TForm).Handle, GWL_WNDPROC));
{$ENDIF}
NewWndProc := MakeObjectInstance(HookWndProc);
{$IFDEF DELPHI_UNICODE}
SetWindowLongPtr((Owner as TForm).Handle, GWL_WNDPROC, LInteger(NewWndProc));
{$ENDIF}
{$IFNDEF DELPHI_UNICODE}
SetWindowLong((Owner as TForm).Handle, GWL_WNDPROC, LInteger(NewWndProc));
{$ENDIF}
end;
procedure TFormShape.PaintForm(Sender: TObject);
var
triangle:array[0..2] of TPoint;
r: TRect;
begin
with (Owner as TForm) do
begin
{Code changes}
Canvas.Pen.Color := FBorderColor;
Canvas.Pen.Width := FBorderWidth;
Canvas.Brush.Color := ColorToRGB(FBalloonColor);
{-----------------------------------}
case BalloonPosition of
bpNone:RoundRect(canvas.handle,0,0,clientrect.right-1,clientrect.bottom-1,balloonellips,balloonellips);
bpTopLeft,bpTopRight:RoundRect(canvas.handle,0,balloonindent,clientrect.right-1,clientrect.bottom-1,balloonellips,balloonellips);
bpLeftTop,bpLeftBottom:RoundRect(canvas.handle,balloonindent,0,clientrect.right-1,clientrect.bottom-1,balloonellips,balloonellips);
bpRightTop,bpRightBottom:RoundRect(canvas.handle,0,0,clientrect.right-balloonindent-1,clientrect.bottom-1,balloonellips,balloonellips);
bpBottomLeft,bpBottomRight:RoundRect(canvas.handle,0,0,clientrect.right-1,clientrect.bottom-balloonindent-1,balloonellips,balloonellips);
end;
case BalloonPosition of
bpTopLeft:begin
triangle[0]:=point(balloonellips,1);
triangle[1]:=point(balloonellips,balloonindent);
triangle[2]:=point(balloonellips+balloonindent-1,balloonindent);
end;
bpTopRight:begin
triangle[0]:=point(clientrect.right-balloonellips-1,1);
triangle[1]:=point(clientrect.right-balloonellips-1,balloonindent);
triangle[2]:=point(clientrect.right-(balloonellips+balloonindent),balloonindent);
end;
bpBottomLeft:begin
triangle[0]:=point(balloonellips,clientrect.bottom-1);
triangle[1]:=point(balloonellips,clientrect.bottom-balloonindent-2);
triangle[2]:=point(balloonellips+balloonindent,clientrect.bottom-balloonindent-2);
end;
bpBottomRight:begin
triangle[0]:=point(clientrect.right-balloonellips-1,clientrect.bottom-1);
triangle[1]:=point(clientrect.right-balloonellips-1,clientrect.bottom-balloonindent-2);
triangle[2]:=point(clientrect.right-(balloonellips+balloonindent)+1,clientrect.bottom-balloonindent-2);
end;
bpLeftTop:begin
triangle[0]:=point(0,balloonellips);
triangle[1]:=point(balloonindent,balloonellips);
triangle[2]:=point(balloonindent,balloonellips+balloonindent);
end;
bpLeftBottom:begin
triangle[0]:=point(1,clientrect.bottom-balloonellips-1);
triangle[1]:=point(balloonindent,clientrect.bottom-balloonellips-1);
triangle[2]:=point(balloonindent,clientrect.bottom-(balloonellips+balloonindent));
end;
bpRightTop:begin
triangle[0]:=point(clientrect.right-1,balloonellips);
triangle[1]:=point(clientrect.right-balloonindent-2,balloonellips);
triangle[2]:=point(clientrect.right-balloonindent-2,balloonellips+balloonindent);
end;
bpRightBottom:begin
triangle[0]:=point(clientrect.right-2,clientrect.bottom-balloonellips-1);
triangle[1]:=point(clientrect.right-balloonindent-2,clientrect.bottom-balloonellips-1);
triangle[2]:=point(clientrect.right-balloonindent-2,clientrect.bottom-(balloonellips+balloonindent));
end;
end;
if BalloonPosition <> bpNone then
begin
Canvas.Polygon(triangle);
Canvas.Pen.Color := BalloonColor;
case BalloonPosition of
bpTopLeft: r:=rect(balloonellips,balloonindent,balloonellips+balloonindent-1,balloonindent);
bpTopRight: r:=rect(clientrect.right-balloonellips,balloonindent,clientrect.right-(balloonellips+balloonindent)+1,balloonindent);
bpLeftTop: r:=rect(balloonindent,balloonellips,balloonindent,balloonindent+balloonellips-1);
bpLeftBottom: r:=rect(balloonindent,clientrect.bottom-balloonellips-1,balloonindent,clientrect.bottom-(balloonindent+balloonellips));
bpRightTop: r:=rect(clientrect.right-balloonindent-2,balloonellips,clientrect.right-balloonindent-2,balloonindent+balloonellips);
bpRightBottom: r:=rect(clientrect.right-balloonindent-2,clientrect.bottom-balloonellips-1,clientrect.right-balloonindent-2,clientrect.bottom-(balloonindent+balloonellips));
bpBottomLeft: r:=rect(balloonellips,clientrect.bottom-balloonindent-2,balloonellips+balloonindent-1,clientrect.bottom-balloonindent-2);
bpBottomRight: r:=rect(clientrect.right-balloonellips,clientrect.bottom-balloonindent-2,clientrect.right-(balloonellips+balloonindent)+1,clientrect.bottom-balloonindent-2);
end;
Canvas.MoveTo(r.right,r.top);
Canvas.LineTo(r.left,r.bottom);
end;
end;
//call existing handler
if (FOnPaintAssigned) then
FOnPaint(sender);
end;
procedure TFormshape.HookWndProc(var Msg: TMessage);
begin
Msg.Result:=CallWindowProc(OldWndProc, (Owner as TForm).Handle, Msg.Msg , Msg.wParam, Msg.lParam);
case Msg.Msg of
WM_NCHITTEST:begin
if fDragBalloon then msg.result:=HTCAPTION;
end;
end;
end;
function TFormShape.GetVersion: string;
var
vn: Integer;
begin
vn := GetVersionNr;
Result := IntToStr(Hi(Hiword(vn)))+'.'+IntToStr(Lo(Hiword(vn)))+'.'+IntToStr(Hi(Loword(vn)))+'.'+IntToStr(Lo(Loword(vn)));
end;
function TFormShape.GetVersionNr: Integer;
begin
Result := MakeLong(MakeWord(BLD_VER,REL_VER),MakeWord(MIN_VER,MAJ_VER));
end;
procedure TFormShape.SetVersion(const Value: string);
begin
end;
end.