forked from PKGeorgiev/Delphi-JsonToDelphiClass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FMX.ConstrainedForm.pas
162 lines (136 loc) · 4.46 KB
/
FMX.ConstrainedForm.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
unit FMX.ConstrainedForm;
// http://stackoverflow.com/a/8044518
interface
uses
System.Classes, System.Types, System.UITypes, FMX.Forms, FMX.Platform, FMX.Types;
type
TFormConstraints = class(TPersistent)
private
FMaxHeight: Integer;
FMaxLeft: Integer;
FMaxWidth: Integer;
FMaxTop: Integer;
FMinHeight: Integer;
FMinLeft: Integer;
FMinWidth: Integer;
FMinTop: Integer;
public
constructor Create;
published
property MaxHeight: Integer read FMaxHeight write FMaxHeight default 0;
property MaxLeft: Integer read FMaxLeft write FMaxLeft default 0;
property MaxWidth: Integer read FMaxWidth write FMaxWidth default 0;
property MaxTop: Integer read FMaxTop write FMaxTop default 0;
property MinHeight: Integer read FMinHeight write FMinHeight default 0;
property MinLeft: Integer read FMinLeft write FMinLeft default 0;
property MinWidth: Integer read FMinWidth write FMinWidth default 0;
property MinTop: Integer read FMinTop write FMinTop default 0;
end;
TConstrainedForm = class(TCustomForm)
private
FConstraints: TFormConstraints;
protected
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
procedure StartWindowResize; override;
procedure StartWindowDrag; override;
published
property Constraints: TFormConstraints read FConstraints write FConstraints;
property BiDiMode;
property Caption;
property Cursor default crDefault;
property BorderStyle default TFmxFormBorderStyle.Sizeable;
property BorderIcons default [TBorderIcon.biSystemMenu, TBorderIcon.biMinimize, TBorderIcon.biMaximize];
property ClientHeight;
property ClientWidth;
property Left;
property Top;
// property Margins;
property Position default TFormPosition.DefaultPosOnly;
property Width;
property Height;
// property ShowActivated default True;
// property StaysOpen default True;
property Transparency;
// property TopMost default False;
property Visible;
property WindowState default TWindowState.wsNormal;
property OnCreate;
property OnDestroy;
property OnClose;
property OnCloseQuery;
property OnActivate;
property OnDeactivate;
property OnResize;
property Fill;
property StyleBook;
property ActiveControl;
property StyleLookup;
property OnPaint;
// XE7
property Padding;
property FormFactor;
property OnKeyDown;
end;
procedure Register;
implementation
{ TFormConstraints }
constructor TFormConstraints.Create;
begin
inherited;
FMaxHeight := 0;
FMaxLeft := 0;
FMaxWidth := 0;
FMaxTop := 0;
FMinHeight := 0;
FMinLeft := 0;
FMinWidth := 0;
FMinTop := 0;
end;
{ TConstrainedForm }
constructor TConstrainedForm.Create(AOwner: TComponent);
begin
FConstraints := TFormConstraints.Create;
inherited;
end;
destructor TConstrainedForm.Destroy;
begin
FConstraints.Free;
inherited;
end;
procedure TConstrainedForm.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
if (FConstraints.FMinWidth > 0) and (AWidth < FConstraints.FMinWidth) then
AWidth := FConstraints.FMinWidth;
if (FConstraints.FMaxWidth > 0) and (AWidth > FConstraints.FMaxWidth) then
AWidth := FConstraints.FMaxWidth;
if (FConstraints.FMinHeight > 0) and (AHeight < FConstraints.FMinHeight) then
AHeight := FConstraints.FMinHeight;
if (FConstraints.FMaxHeight > 0) and (AHeight > FConstraints.FMaxHeight) then
AHeight := FConstraints.FMaxHeight;
if (FConstraints.FMinLeft > 0) and (ALeft < FConstraints.FMinLeft) then
ALeft := FConstraints.FMinLeft;
if (FConstraints.FMaxLeft > 0) and (ALeft > FConstraints.FMaxLeft) then
ALeft := FConstraints.FMaxLeft;
if (FConstraints.FMinTop > 0) and (ATop < FConstraints.FMinTop) then
ATop := FConstraints.FMinTop;
if (FConstraints.FMaxTop > 0) and (ATop > FConstraints.FMaxTop) then
ATop := FConstraints.FMaxTop;
FWinService.SetWindowRect(Self, RectF(ALeft, ATop, ALeft + AWidth, ATop + AHeight));
inherited SetBounds(ALeft, ATop, AWidth, AHeight);
end;
procedure TConstrainedForm.StartWindowDrag;
begin
inherited;
end;
procedure TConstrainedForm.StartWindowResize;
begin
inherited;
end;
procedure Register;
begin
RegisterClass(TConstrainedForm);
end;
end.