-
Notifications
You must be signed in to change notification settings - Fork 7
/
SizeControl.pas
1426 lines (1276 loc) · 46 KB
/
SizeControl.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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit SizeControl;
(* ---------------------------------------------------------------------------*}
Component Name: TSizeCtrl
Module: SizeControl
Description: Enables both moving and resizing of controls at runtime.
Version: 7.2
Date: 3-SEP-2006
Compiler: Delphi 3, Delphi 7
Author: Angus Johnson, angusj-AT-myrealbox-DOT-com
Copyright: © 1997-2006 Angus Johnson
(* ---------------------------------------------------------------------------*}
(* ---------------------------------------------------------------------------*}
BASIC USAGE:
1. Add a TSizeCtrl component (SizeCtrl1) to your form.
2. Set SizeCtrl1 properties (button colors etc) as desired.
3. Assign event methods (start, during & end size/move events) as desired.
4. In the form's OnCreate method, SizeCtrl1.RegisterControl() all possible targets.
5. In an assigned menuitem method, toggle the SizeCtrl1.Enabled property.
6. Once enabled:
* Click or Tab to select targets.
* Hold the Shift key down to select multiple targets.
* Resize targets by click & dragging a target's resize buttons
or by holding the Shift key down while use the arrow keys.
* Move controls by click & dragging a target or by using the arrow keys.
{* ---------------------------------------------------------------------------*)
(* ---------------------------------------------------------------------------*}
MISCELLANEOUS NOTES:
Capturing the WM_SETCURSOR messages of Listview headers requires hooking
the header's message handler too. I don't think this minor improvement in
cursor management justifies the considerable extra programming effort.
{* ---------------------------------------------------------------------------*)
interface
{$R SIZECONTROL}
uses
Windows, Messages, SysUtils, Classes, Controls, ExtCtrls,
Graphics, Forms, TypInfo, Menus;
type
TSizeCtrl = class;
TTargetObj = class;
TBtnPos = (bpLeft, bpTopLeft, bpTop, bpTopRight,
bpRight, bpBottomRight, bpBottom, bpBottomLeft);
TBtnPosSet = set of TBtnPos;
TSCState = (scsReady, scsMoving, scsSizing);
TStartEndEvent = procedure (Sender: TObject; State: TSCState) of object;
TDuringEvent = procedure (Sender: TObject; dx, dy: integer; State: TSCState) of object;
TMouseDownEvent = procedure (Sender: TObject;
Target: TControl; TargetPt: TPoint; var handled: boolean) of object;
TSetCursorEvent = procedure (Sender: TObject;
Target: TControl; TargetPt: TPoint; var handled: boolean) of object;
TContextPopupEvent = procedure(Sender: TObject; MousePos: TPoint; var Handled: Boolean) of object;
//TSizeBtn is used internally by TSizeCtrl.
//There are 8 TSizeBtns for each target which are the target's resize handles.
TSizeBtn = class(TCustomControl)
private
fTargetObj: TTargetObj;
fPos: TBtnPos;
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure UpdateBtnCursorAndColor;
procedure MouseDown(Button: TMouseButton;
Shift: TShiftState; X, Y: Integer); override;
public
constructor Create(TargetObj: TTargetObj;
BtnPos: TBtnPos); {$IFNDEF VER100} reintroduce; {$ENDIF}
end;
//TRegisteredObj is used internally by TSizeCtrl. Each TRegisteredObj
//contains info about a possible target control.
TRegisteredObj = class
fSizeCtrl: TSizeCtrl; //the owner of TRegisteredObj
fControl: TControl;
fHooked: boolean;
fOldWindowProc: TWndMethod;
fOldClickMethod : TMethod;
procedure Hook;
procedure UnHook;
procedure NewWindowProc(var Msg : TMessage);
public
constructor Create(aSizeCtrl: TSizeCtrl;
aControl: TControl); {$IFNDEF VER100} reintroduce; {$ENDIF}
destructor Destroy; override;
end;
//TTargetObj is the container for each current target, and contains the 8
//TSizeBtn objects. Any number of TTargetObj's can be contained by TSizeCtrl.
TTargetObj = class
private
fSizeCtrl: TSizeCtrl; //the owner of TTargetObj
fTarget: TControl;
fBtns: array [TBtnPos] of TSizeBtn;
fFocusRect: TRect;
fStartRec: TRect;
procedure Hide;
procedure Show;
procedure Update;
procedure StartFocus;
procedure MoveFocus(dx,dy: integer);
procedure SizeFocus(dx,dy: integer; BtnPos: TBtnPos);
procedure EndFocus;
procedure DrawRect(dc: hDC);
public
constructor Create(aSizeCtrl: TSizeCtrl;
aTarget: TControl); {$IFNDEF VER100} reintroduce; {$ENDIF}
destructor Destroy; override;
end;
TSizeCtrl = class(TComponent)
private
fTargetList: TList; //list of TTargetObj (current targets)
fRegList: TList; //list of TRegisteredObj (possible targets)
fState: TSCState;
fMoveOnly: boolean;
fClipRec: TRect;
fStartPt: TPoint;
fEnabledBtnColor: TColor;
fDisabledBtnColor: TColor;
fValidBtns: TBtnPosSet;
fMultiResize: boolean;
fEnabled: boolean;
fCapturedCtrl: TControl;
fCapturedBtnPos: TBtnPos;
fGridSize: integer;
fOldWindowProc: TWndMethod;
fEscCancelled: boolean;
fParentForm: TCustomForm;
fHandle: THandle;
fPopupMenu: TPopupMenu;
fOnContextPopup: TContextPopupEvent;
fLMouseDownPending: boolean;
fStartEvent: TStartEndEvent;
fDuringEvent: TDuringEvent;
fEndEvent: TStartEndEvent;
fTargetChangeEvent: TNotifyEvent;
fOnMouseDown: TMouseDownEvent;
fOnSetCursor: TSetCursorEvent;
fOnKeyDown: TKeyEvent;
function GetTargets(index: integer):TControl;
function GetTargetCount: integer;
procedure SetEnabled(Value: boolean);
procedure WinProc(var Msg : TMessage);
procedure FormWindowProc(var Msg : TMessage);
procedure DoWindowProc(DefaultProc: TWndMethod; var Msg : TMessage);
procedure DrawRect;
procedure SetMoveOnly(Value: boolean);
function IsValidSizeBtn(BtnPos: TBtnPos): boolean;
function IsValidMove: boolean;
procedure SetMultiResize(Value: boolean);
procedure SetPopupMenu(Value: TPopupMenu);
procedure DoPopupMenuStuff;
procedure SetEnabledBtnColor(aColor: TColor);
procedure SetDisabledBtnColor(aColor: TColor);
function RegisteredCtrlFromPt(screenPt: TPoint): TControl;
procedure DoMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState);
procedure DoMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState);
procedure DoMouseMove(Sender: TObject; Shift: TShiftState);
protected
procedure Hide;
procedure Show;
procedure UpdateBtnCursors;
procedure MoveTargets(dx, dy: integer);
procedure SizeTargets(dx, dy: integer);
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
function DoKeyDown(var Message: TWMKey): Boolean;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
//Update: it is the responsibility of the component user to call Update
//if the target(s) are moved or resized independently of this control
//(eg if the form is resized and targets are aligned with it.)
procedure Update;
//RegisterControl: Register potential target controls with TSizeCtrl
function RegisterControl(Control: TControl): integer;
procedure UnRegisterControl(Control: TControl);
procedure UnRegisterAll;
function RegisteredIndex(Control: TControl): integer;
//AddTarget: Add any number of targets to TSizeCtrl so they can be
//resized or moved together.
//(nb: The programmer doesn't normally need to call this method directly
//since TSizeCtrl will call it whenever a target is clicked.)
function AddTarget(Control: TControl): integer;
procedure DeleteTarget(Control: TControl);
procedure ClearTargets;
function TargetIndex(Control: TControl): integer;
function TargetCtrlFromPt(screenPt: TPoint): TControl;
//Enabled: This key property should be self-explanatory.
property Enabled: boolean read fEnabled write SetEnabled;
//Targets: used to access individual targets (read-only)
property Targets[index: integer]: TControl read GetTargets;
property TargetCount: integer read GetTargetCount;
published
//MoveOnly: ie prevents resizing
property MoveOnly: boolean read fMoveOnly write SetMoveOnly;
//BtnColor: Color of grab-handle buttons
property BtnColor: TColor read fEnabledBtnColor write SetEnabledBtnColor;
//BtnColorDisabled: eg grab buttons along aligned edges of target controls
property BtnColorDisabled: TColor read fDisabledBtnColor
write SetDisabledBtnColor;
//GridSize: aligns mouse moved/resized controls to nearest grid dimensions
property GridSize: integer read fGridSize write fGridSize;
//MultiTargetResize: Resizing of multiple targets is allowed by default
//as long as this isn't impeded by specific Target control alignments
property MultiTargetResize: boolean read fMultiResize write SetMultiResize;
property PopupMenu: TPopupMenu read fPopupMenu write SetPopupMenu;
//Self-explanatory Events ...
property OnStartSizeMove: TStartEndEvent read fStartEvent write fStartEvent;
property OnDuringSizeMove: TDuringEvent read fDuringEvent write fDuringEvent;
property OnEndSizeMove: TStartEndEvent read fEndEvent write fEndEvent;
property OnTargetChange: TNotifyEvent
read fTargetChangeEvent write fTargetChangeEvent;
property OnKeyDown: TKeyEvent read fOnKeyDown write fOnKeyDown;
property OnMouseDown: TMouseDownEvent read fOnMouseDown write fOnMouseDown;
property OnSetCursor: TSetCursorEvent read fOnSetCursor write fOnSetCursor;
property OnContextPopup: TContextPopupEvent read fOnContextPopup write fOnContextPopup;
end;
const
BTNSIZE = 5;
MINWIDTH = 1; //minimum target width (could make this a property later)
MINHEIGHT = 1; //minimum target height
CM_LMOUSEDOWN = WM_USER + $1;
CM_RMOUSEDOWN = WM_USER + $2;
procedure Register;
implementation
type
THackedControl = class(TControl);
THackedWinControl = class(TWinControl);
procedure Register;
begin
RegisterComponents('Samples', [TSizeCtrl]);
end;
{$IFDEF VER100} type TAlignSet = set of TAlign; {$ENDIF}
//turn warnings off concerning unsafe typecasts since we know they're safe...
{$WARNINGS OFF}
//------------------------------------------------------------------------------
// Miscellaneous functions
//------------------------------------------------------------------------------
function max(int1, int2: integer): integer;
begin
if int1 > int2 then result := int1 else result := int2;
end;
//------------------------------------------------------------------------------
function IsVisible(Control: TControl): boolean;
begin
result := true;
while assigned(Control) do
if Control is TCustomForm then exit
else if not Control.Visible then break
else Control := Control.Parent;
result := false;
end;
//------------------------------------------------------------------------------
function GetBoundsAsScreenRect(Control: TControl): TRect;
begin
//GetBoundsAsScreenRect() assumes 'Control' is both assigned and has a parent.
//Not all TControls have handles (ie only TWinControls) so ...
with Control do
begin
result.TopLeft := parent.ClientToScreen(BoundsRect.TopLeft);
result.Right := result.Left + width;
result.Bottom := result.Top + height;
end;
end;
//------------------------------------------------------------------------------
function PointIsInControl(screenPt: TPoint; Control: TControl): boolean;
begin
//PointIsInControl() assumes 'Control' is both assigned and has a parent.
result := PtInRect(GetBoundsAsScreenRect(Control), screenPt);
end;
//------------------------------------------------------------------------------
procedure AlignToGrid(Ctrl: TControl; ProposedBoundsRect: TRect; GridSize: integer);
begin
//AlignToGrid() assumes 'Control' is assigned.
if (GridSize > 1) then
begin
//simplify rounding ...
OffsetRect(ProposedBoundsRect,GridSize div 2, GridSize div 2);
dec(ProposedBoundsRect.Left, ProposedBoundsRect.Left mod GridSize);
dec(ProposedBoundsRect.Top, ProposedBoundsRect.Top mod GridSize);
dec(ProposedBoundsRect.Right, ProposedBoundsRect.Right mod GridSize);
dec(ProposedBoundsRect.Bottom, ProposedBoundsRect.Bottom mod GridSize);
end;
with ProposedBoundsRect do Ctrl.SetBounds(left,top,right, bottom)
end;
//-----------------------------------------------------------------------
function ShiftKeyIsPressed: boolean;
begin
result := GetKeyState(VK_SHIFT) < 0;
end;
//-----------------------------------------------------------------------
function CtrlKeyIsPressed: boolean;
begin
result := GetKeyState(VK_CONTROL) < 0;
end;
//------------------------------------------------------------------------------
// TRegisteredObj functions
//------------------------------------------------------------------------------
constructor TRegisteredObj.Create(aSizeCtrl: TSizeCtrl; aControl: TControl);
begin
inherited Create;
fSizeCtrl := aSizeCtrl;
fControl := aControl;
if fSizeCtrl.Enabled then Hook;
end;
//------------------------------------------------------------------------------
destructor TRegisteredObj.Destroy;
begin
UnHook;
inherited Destroy;
end;
//------------------------------------------------------------------------------
procedure TRegisteredObj.Hook;
var
meth: TMethod;
begin
if fHooked then exit;
fOldWindowProc := fControl.WindowProc;
fControl.WindowProc := NewWindowProc;
//The following is needed to block OnClick events when TSizeCtrl is enabled.
//(If compiling with Delphi 3, you'll need to block OnClick events manually.)
{$IFNDEF VER100}
if IsPublishedProp(fControl, 'OnClick') then
begin
meth := GetMethodProp(fControl, 'OnClick');
fOldClickMethod.Code := meth.Code;
fOldClickMethod.Data := meth.Data;
meth.Code := nil;
meth.Data := nil;
SetMethodProp(fControl, 'OnClick', meth);
end;
{$ENDIF}
fHooked := true;
end;
//------------------------------------------------------------------------------
procedure TRegisteredObj.UnHook;
var
meth: TMethod;
begin
if not fHooked then exit;
fControl.WindowProc := fOldWindowProc;
{$IFNDEF VER100}
try
if IsPublishedProp(fControl, 'OnClick') then
begin
meth.Code := fOldClickMethod.Code;
meth.Data := fOldClickMethod.Data;
SetMethodProp(fControl, 'OnClick', meth);
end;
except
end;
{$ENDIF}
fHooked := false;
end;
//------------------------------------------------------------------------------
procedure TRegisteredObj.NewWindowProc(var Msg : TMessage);
begin
fSizeCtrl.DoWindowProc(fOldWindowProc, Msg);
end;
//------------------------------------------------------------------------------
// TSizeBtn methods
//------------------------------------------------------------------------------
constructor TSizeBtn.Create(TargetObj: TTargetObj; BtnPos: TBtnPos);
begin
inherited create(nil);
fTargetObj := TargetObj;
fPos := BtnPos;
width := BTNSIZE;
height := BTNSIZE;
Visible := false;
UpdateBtnCursorAndColor;
end;
//------------------------------------------------------------------------------
procedure TSizeBtn.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.ExStyle := Params.ExStyle or WS_EX_NOPARENTNOTIFY;
end;
//------------------------------------------------------------------------------
procedure TSizeBtn.UpdateBtnCursorAndColor;
begin
if not (fPos in fTargetObj.fSizeCtrl.fValidBtns) or
fTargetObj.fSizeCtrl.fMoveOnly then
begin
Cursor := crDefault;
Color := fTargetObj.fSizeCtrl.fDisabledBtnColor;
end else
begin
case fPos of
bpLeft,bpRight: Cursor := crSizeWE;
bpTop, bpBottom: Cursor := crSizeNS;
bpTopLeft, bpBottomRight: Cursor := crSizeNWSE;
bpTopRight, bpBottomLeft: Cursor := crSizeNESW;
end;
Color := fTargetObj.fSizeCtrl.fEnabledBtnColor;
end;
end;
//------------------------------------------------------------------------------
procedure TSizeBtn.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
fTargetObj.fSizeCtrl.DoMouseDown(self, Button, Shift);
end;
//------------------------------------------------------------------------------
// TTargetObj methods
//------------------------------------------------------------------------------
constructor TTargetObj.Create(aSizeCtrl: TSizeCtrl; aTarget: TControl);
var
i: TBtnPos;
begin
inherited Create;
fSizeCtrl := aSizeCtrl;
fTarget := aTarget;
for i := low(TBtnPos) to high(TBtnPos) do
fBtns[i] := TSizeBtn.Create(self, i);
end;
//------------------------------------------------------------------------------
destructor TTargetObj.Destroy;
var
i: TBtnPos;
begin
for i := low(TBtnPos) to high(TBtnPos) do fBtns[i].Free;
inherited Destroy;
end;
//------------------------------------------------------------------------------
procedure TTargetObj.Hide;
var
i: TBtnPos;
begin
for i := low(TBtnPos) to high(TBtnPos) do fBtns[i].Visible := false;
//to avoid the buttons messing up the Size-Move Rect ...
if fTarget is TWinControl then fTarget.Repaint
else fTarget.Parent.Repaint;
end;
//------------------------------------------------------------------------------
procedure TTargetObj.Show;
var
i: TBtnPos;
begin
for i := low(TBtnPos) to high(TBtnPos) do fBtns[i].Visible := true;
end;
//------------------------------------------------------------------------------
procedure TTargetObj.Update;
var
i: TBtnPos;
parentForm: TCustomForm;
tl: TPoint;
bsDiv2: integer;
begin
parentForm := fSizeCtrl.fParentForm;
if not assigned(parentForm) then exit;
//get tl of Target relative to parentForm ...
tl := GetBoundsAsScreenRect(fTarget).TopLeft;
tl := parentForm.ScreenToClient(tl);
bsDiv2 := (BTNSIZE div 2);
for i := low(TBtnPos) to high(TBtnPos) do
begin
fBtns[i].ParentWindow := parentForm.Handle; //ie keep btns separate !!!
fBtns[i].Left := tl.X - bsDiv2;
case i of
bpTop, bpBottom:
fBtns[i].Left := fBtns[i].Left + (fTarget.Width div 2);
bpRight, bpTopRight, bpBottomRight:
fBtns[i].Left := fBtns[i].Left + fTarget.Width -1;
end;
fBtns[i].Top := tl.Y - bsDiv2;
case i of
bpLeft, bpRight:
fBtns[i].Top := fBtns[i].Top + (fTarget.Height div 2);
bpBottomLeft, bpBottom, bpBottomRight:
fBtns[i].Top := fBtns[i].Top + fTarget.Height -1;
end;
//force btns to the top ...
SetWindowPos(fBtns[i].Handle, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE);
end;
end;
//------------------------------------------------------------------------------
procedure TTargetObj.StartFocus;
begin
fFocusRect := GetBoundsAsScreenRect(fTarget);
fStartRec := fFocusRect;
end;
//------------------------------------------------------------------------------
procedure TTargetObj.MoveFocus(dx,dy: integer);
begin
fFocusRect := fStartRec;
offsetRect(fFocusRect, dx,dy);
end;
//------------------------------------------------------------------------------
procedure TTargetObj.SizeFocus(dx,dy: integer; BtnPos: TBtnPos);
begin
fFocusRect := fStartRec;
case BtnPos of
bpLeft: inc(fFocusRect.Left, dx);
bpTopLeft:
begin
inc(fFocusRect.Left, dx);
inc(fFocusRect.Top, dy);
end;
bpTop: inc(fFocusRect.Top, dy);
bpTopRight:
begin
inc(fFocusRect.Right, dx);
inc(fFocusRect.Top, dy);
end;
bpRight: inc(fFocusRect.Right, dx);
bpBottomRight:
begin
inc(fFocusRect.Right, dx);
inc(fFocusRect.Bottom, dy);
end;
bpBottom: inc(fFocusRect.Bottom, dy);
bpBottomLeft:
begin
inc(fFocusRect.Left, dx);
inc(fFocusRect.Bottom, dy);
end;
end;
end;
//------------------------------------------------------------------------------
procedure TTargetObj.EndFocus;
begin
//update target position ...
fFocusRect.TopLeft := fTarget.Parent.ScreenToClient(fFocusRect.TopLeft);
fFocusRect.BottomRight := fTarget.Parent.ScreenToClient(fFocusRect.BottomRight);
with fFocusRect do
AlignToGrid(fTarget, Rect(Left, top, max(MINWIDTH, right - left),
max(MINHEIGHT, bottom - top)), fSizeCtrl.fGridSize);
Update;
fTarget.Invalidate;
end;
//------------------------------------------------------------------------------
procedure TTargetObj.DrawRect(dc: hDC);
begin
DrawFocusRect(dc, fFocusRect);
end;
//------------------------------------------------------------------------------
// TSizeCtrl methods
//------------------------------------------------------------------------------
constructor TSizeCtrl.Create(AOwner: TComponent);
begin
if not (aOwner is TWinControl) then
raise Exception.Create('TSizeCtrl.Create: Owner must be a TWinControl');
inherited Create(AOwner);
fTargetList := TList.Create;
fRegList := TList.Create;
fEnabledBtnColor := clNavy;
fDisabledBtnColor := clGray;
fMultiResize := true;
fValidBtns := [bpLeft, bpTopLeft,
bpTop, bpTopRight, bpRight, bpBottomRight, bpBottom, bpBottomLeft];
fHandle := AllocateHWnd(WinProc);
{$IFDEF VER100}
screen.Cursors[crSize] := loadcursor(hInstance,'NSEW');
{$ENDIF}
end;
//------------------------------------------------------------------------------
destructor TSizeCtrl.Destroy;
begin
if assigned(fTargetList) then
begin
DeallocateHWnd(fHandle);
UnRegisterAll;
fTargetList.Free;
fRegList.Free;
end;
inherited Destroy;
end;
//------------------------------------------------------------------------------
procedure TSizeCtrl.SetEnabled(Value: boolean);
var
i: integer;
begin
if Value = fEnabled then exit;
fParentForm := GetParentForm(TWinControl(owner));
if fParentForm = nil then exit;
fEnabled := Value;
ClearTargets;
if fEnabled then
begin
//hook all registered controls and disable their OnClick events ...
for i := 0 to fRegList.Count -1 do TRegisteredObj(fRegList[i]).Hook;
//hook the parent form too ...
fOldWindowProc := fParentForm.WindowProc;
fParentForm.WindowProc := FormWindowProc;
end else
begin
//unhook all registered controls and reenable their OnClick events ...
for i := 0 to fRegList.Count -1 do TRegisteredObj(fRegList[i]).UnHook;
//unhook the parent form too ...
fParentForm.WindowProc := fOldWindowProc;
end;
end;
//------------------------------------------------------------------------------
procedure TSizeCtrl.FormWindowProc(var Msg : TMessage);
begin
DoWindowProc(fOldWindowProc, Msg);
end;
//------------------------------------------------------------------------------
//TSizeCtrl's own message handler to process CM_CUSTOM_MSE_DOWN message
procedure TSizeCtrl.WinProc(var Msg : TMessage);
var
Button: TMouseButton;
ShiftState: TShiftState;
begin
with Msg do
if Msg = CM_LMOUSEDOWN then
try
fLMouseDownPending := false;
if bool(WParam) then Button := mbLeft else Button := mbRight;
if bool(LParam) then ShiftState := [ssShift] else ShiftState := [];
DoMouseDown(nil, Button, ShiftState);
except
Application.HandleException(Self);
end
else
Result := DefWindowProc(fHandle, Msg, wParam, lParam);
end;
//------------------------------------------------------------------------------
//WindowProc for the 'hooked' form and all 'hooked' controls
procedure TSizeCtrl.DoWindowProc(DefaultProc: TWndMethod; var Msg : TMessage);
var
i: integer;
ShiftState: TShiftState;
controlPt, screenPt: TPoint;
regCtrl: TControl;
handled: boolean;
//this seems the only reasonably simple way of managing both 'owned' and
//'notified' WM_LBUTTONDOWN messages ...
procedure PostMouseDownMessage(isLeftBtn, shiftKeyPressed: boolean);
begin
if fLMouseDownPending then exit;
if assigned(fOnMouseDown) then
begin
getCursorPos(screenPt);
regCtrl := RegisteredCtrlFromPt(screenPt);
if assigned(regCtrl) then
begin
handled := false;
controlPt := regCtrl.ScreenToClient(screenPt);
fOnMouseDown(self, regCtrl, controlPt, handled);
if handled then exit;
end;
end;
fLMouseDownPending := true;
PostMessage(fHandle, CM_LMOUSEDOWN,ord(isLeftBtn),ord(shiftKeyPressed));
end;
begin
case Msg.Msg of
WM_MOUSEFIRST .. WM_MOUSELAST:
begin
ShiftState := KeysToShiftState(Word(TWMMouse(Msg).Keys));
case Msg.Msg of
WM_LBUTTONDOWN: PostMouseDownMessage(true, ssShift in ShiftState);
WM_RBUTTONDOWN: DoPopupMenuStuff;
WM_MOUSEMOVE: DoMouseMove(nil, ShiftState);
WM_LBUTTONUP: DoMouseUp(nil, mbLeft, ShiftState);
//Could also add event handlers for right click events here.
end;
Msg.Result := 0;
end;
WM_PARENTNOTIFY:
if not (TWMParentNotify(Msg).Event in [WM_CREATE, WM_DESTROY]) then
begin
if ShiftKeyIsPressed then ShiftState := [ssShift] else ShiftState := [];
case TWMParentNotify(Msg).Event of
WM_LBUTTONDOWN: PostMouseDownMessage(true, ssShift in ShiftState);
end;
Msg.Result := 0;
end;
WM_SETCURSOR:
if (HIWORD(Msg.lParam) <> 0) then
begin
Msg.Result := 1;
getCursorPos(screenPt);
regCtrl := RegisteredCtrlFromPt(screenPt);
handled := false;
if assigned(fOnSetCursor) and assigned(regCtrl) then
begin
controlPt := regCtrl.ScreenToClient(screenPt);
fOnSetCursor(self, RegisteredCtrlFromPt(screenPt), controlPt, handled);
end;
if handled then //do nothing
else if TargetIndex(regCtrl) >= 0 then
begin
if not IsValidMove then DefaultProc(Msg)
else windows.SetCursor(screen.Cursors[crSize]);
end else if assigned(regCtrl) then
windows.SetCursor(screen.Cursors[crHandPoint])
else
DefaultProc(Msg);
end else
DefaultProc(Msg);
WM_GETDLGCODE: Msg.Result := DLGC_WANTTAB or DLGC_WANTARROWS;
WM_KEYDOWN:
begin
Msg.Result := 0;
if DoKeyDown(TWMKey(Msg)) then exit;
case Msg.WParam of
VK_UP:
if ShiftKeyIsPressed then
begin
SizeTargets(0,-1);
if assigned(fEndEvent) then fEndEvent(self, scsSizing);
end else
begin
MoveTargets(0,-1);
if assigned(fEndEvent) then fEndEvent(self, scsMoving);
end;
VK_DOWN:
if ShiftKeyIsPressed then
begin
SizeTargets(0,+1);
if assigned(fEndEvent) then fEndEvent(self, scsSizing);
end else
begin
MoveTargets(0,+1);
if assigned(fEndEvent) then fEndEvent(self, scsMoving);
end;
VK_LEFT:
if ShiftKeyIsPressed then
begin
SizeTargets(-1,0);
if assigned(fEndEvent) then fEndEvent(self, scsSizing);
end else
begin
MoveTargets(-1,0);
if assigned(fEndEvent) then fEndEvent(self, scsMoving);
end;
VK_RIGHT:
if ShiftKeyIsPressed then
begin
SizeTargets(+1,0);
if assigned(fEndEvent) then fEndEvent(self, scsSizing);
end else
begin
MoveTargets(+1,0);
if assigned(fEndEvent) then fEndEvent(self, scsMoving);
end;
VK_TAB:
begin
if fRegList.Count = 0 then exit
else if targetCount = 0 then
AddTarget(TRegisteredObj(fRegList[0]).fControl) else
begin
i := RegisteredIndex(Targets[0]);
if ShiftKeyIsPressed then dec(i) else inc(i);
if i < 0 then i := fRegList.Count -1
else if i = fRegList.Count then i := 0;
ClearTargets;
AddTarget(TRegisteredObj(fRegList[i]).fControl);
end;
end;
VK_ESCAPE:
//ESCAPE is used for both -
// 1. cancelling a mouse move/resize operation, and
// 2. selecting the parent of the currenctly selected target
if fState <> scsReady then
begin
fEscCancelled := true;
DoMouseUp(nil, mbLeft, []);
end else begin
if (targetCount = 0) then exit;
i := RegisteredIndex(Targets[0].Parent);
ClearTargets;
if i >= 0 then
AddTarget(TRegisteredObj(fRegList[i]).fControl);
end;
end;
end;
WM_KEYUP: Msg.Result := 0;
WM_CHAR: Msg.Result := 0;
else DefaultProc(Msg);
end;
end;
//------------------------------------------------------------------------------
function TSizeCtrl.DoKeyDown(var Message: TWMKey): Boolean;
var
ShiftState: TShiftState;
begin
Result := true;
if fParentForm.KeyPreview and
THackedWinControl(fParentForm).DoKeyDown(Message) then Exit;
if Assigned(fOnKeyDown) then
with Message do
begin
ShiftState := KeyDataToShiftState(KeyData);
fOnKeyDown(Self, CharCode, ShiftState);
if CharCode = 0 then Exit;
end;
Result := False;
end;
//------------------------------------------------------------------------------
function TSizeCtrl.GetTargets(index: integer): TControl;
begin
if (index < 0) or (index >= TargetCount) then
result := nil else
result := TTargetObj(fTargetList[index]).fTarget;
end;
//------------------------------------------------------------------------------
function TSizeCtrl.TargetIndex(Control: TControl): integer;
var
i: integer;
begin
result := -1;
if assigned(Control) then
for i := 0 to fTargetList.Count -1 do
if TTargetObj(fTargetList[i]).fTarget = Control then
begin
result := i;
break;
end;
end;
//------------------------------------------------------------------------------
function TSizeCtrl.AddTarget(Control: TControl): integer;
var
TargetObj: TTargetObj;
begin
result := -1;
if (csDestroying in ComponentState) or (fState <> scsReady) then exit;
result := TargetIndex(Control);
if not assigned(Control) or not Control.Visible or
(Control is TCustomForm) or (result >= 0) then exit;
result := fTargetList.Count;
TargetObj := TTargetObj.Create(self, Control);
fTargetList.Add(TargetObj);
UpdateBtnCursors;
TargetObj.Update;
TargetObj.Show;
RegisterControl(Control);
fParentForm.ActiveControl := nil;
if assigned(fTargetChangeEvent) then fTargetChangeEvent(self);
end;
//------------------------------------------------------------------------------
procedure TSizeCtrl.DeleteTarget(Control: TControl);
var
i: integer;
begin
i := TargetIndex(Control);
if i < 0 then exit;
TTargetObj(fTargetList[i]).Free;
fTargetList.Delete(i);
UpdateBtnCursors;
if assigned(fTargetChangeEvent) then fTargetChangeEvent(self);
end;
//------------------------------------------------------------------------------
procedure TSizeCtrl.ClearTargets;
var
i: integer;
begin
if fTargetList.Count = 0 then exit;
for i := 0 to fTargetList.Count -1 do TTargetObj(fTargetList[i]).Free;
fTargetList.Clear;
if (csDestroying in ComponentState) then exit;
UpdateBtnCursors;
if assigned(fTargetChangeEvent) then fTargetChangeEvent(self);
end;
//------------------------------------------------------------------------------
function TSizeCtrl.RegisterControl(Control: TControl): integer;
var
RegisteredObj: TRegisteredObj;
begin
if not IsVisible(Control) then
begin
result := -1;
exit;
end;
result := RegisteredIndex(Control);
if result >= 0 then exit;
result := fRegList.Count;
RegisteredObj := TRegisteredObj.Create(self, Control);
fRegList.Add(RegisteredObj);
end;
//------------------------------------------------------------------------------
procedure TSizeCtrl.UnRegisterControl(Control: TControl);
var
i: integer;
begin
//first, make sure it's not a current target ...
DeleteTarget(Control);
//now unregister it ...
i := RegisteredIndex(Control);
if i < 0 then exit;
TRegisteredObj(fRegList[i]).Free;
fRegList.Delete(i);
end;
//------------------------------------------------------------------------------
procedure TSizeCtrl.UnRegisterAll;
var
i: integer;
begin
//first, clear any targets
ClearTargets;
//now, clear all registered controls ...
for i := 0 to fRegList.Count -1 do TRegisteredObj(fRegList[i]).Free;
fRegList.Clear;