-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormLocation.pas
95 lines (83 loc) · 2.94 KB
/
FormLocation.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
unit FormLocation;
interface
uses Windows, Controls, Forms, Registry, SysUtils;
type
TStoreType = (stLeft, stTop, stWidth, stHeight, stState);
TStoreOptions = set of TStoreType;
const
soAll = [stLeft, stTop, stWidth, stHeight, stState];
soPosOnly = [stLeft, stTop];
soSizeOnly = [stWidth, stHeight, stState];
procedure StoreFormLocation(const Reg: TRegistry; const FormKey: string;
AForm: TForm; StoreOptions: TStoreOptions);
function RetrieveFormLocation(const Reg: TRegistry; const FormKey: string;
AForm: TForm; var Params: TCreateParams): Boolean;
procedure FitFormOnScreen(var Params: TCreateParams);
implementation
procedure StoreFormLocation;
var
WindowPlacement: TWINDOWPLACEMENT;
begin
with Reg do
begin
if OpenKey(FormKey, True) then
begin
WindowPlacement.length := sizeof(WindowPlacement);
if not GetWindowPlacement(AForm.Handle, @WindowPlacement) then
raise Exception.Create('GetWindowPlacement() failed for ' + FormKey);
with WindowPlacement.rcNormalPosition do
begin
if stLeft in StoreOptions then WriteInteger('X', Left);
if stTop in StoreOptions then WriteInteger('Y', Top);
if stWidth in StoreOptions then WriteInteger('W', Right - Left);
if stHeight in StoreOptions then WriteInteger('H', Bottom - Top);
if stState in StoreOptions then WriteInteger('S', Ord(Aform.WindowState));
end;
end;
end;
end;
function RetrieveFormLocation;
var HasLoc, HasSize: boolean;
begin
Result := False;
with Reg do
begin
if OpenKey(FormKey, False) then
begin
HasLoc := ValueExists('X');
if HasLoc then
Params.X := ReadInteger('X');
if ValueExists('Y') then
Params.Y := ReadInteger('Y');
HasSize := ValueExists('W');
if HasSize then
Params.Width := ReadInteger('W');
if ValueExists('H') then
Params.Height := ReadInteger('H');
if ValueExists('S') then
AForm.WindowState := TWindowState(ReadInteger('S'));
if HasLoc then
begin
if HasSize then
AForm.Position := poDefault
else
AForm.Position := poDefaultPosOnly;
end else if HasSize then
AForm.Position := poDefaultSizeOnly;
end;
end;
end;
procedure FitFormOnScreen(var Params: TCreateParams);
begin
if Params.X < 0 then params.X := 0;
if Params.Y < 0 then params.Y := 0;
if params.Width > screen.Width then
params.Width := screen.Width;
if params.Height > screen.Height then
params.Height := screen.Height;
if params.Y + params.Height > screen.Height then
params.Y := screen.Height - params.Height;
if params.X + params.Width > screen.Width then
params.X := screen.Width - params.Width;
end;
end.