Author: William Gerbert
How to save/restore the form state in/from the registry
Answer:
You may try the following unit. There is one thing you may have to handle
separately somehow: the mainform is never actually minimized, so its Windowstate is
never wsMinimized, unless you set it in code. When you minimize the main form it is
hidden and the Application window is minimized instead. You can check whether it is
minimized via
1 if IsIconic(Application.handle) then
2 {... app is minimized}
3
4 unit RegWinset;
5
6 interface
7
8 uses
9 Registry, Forms;
10
11 procedure SaveWindowstate(ini: TRegInifile; form: TForm);
12 procedure RestoreWindowstate(ini: TRegInifile; form: TForm);
13 procedure SaveWindowstateEx(ini: TRegInifile; form: TForm; const section: string);
14 procedure RestoreWindowstateEx(ini: TRegInifile; form: TForm; const section:
15 string);
16
17 implementation
18
19 uses
20 TypeInfo, Windows;
21
22 const
23 sSettings = 'Settings';
24 sLeft = 'Left';
25 sTop = 'Top';
26 sWidth = 'Width';
27 sHeight = 'Height';
28 sState = 'State';
29
30 {Procedure SaveWindowStateEx;
31
32 Parameters:
33 ini: inifile to save the settings in
34 form: form to save the settings for
35 section: section to use for the settings
36
37 Call method: static
38 Description: Saves the windows position and size to the INI file.
39 Error Conditions: none
40 Created: 03.07.97 16:34:34 by P. Below}
41
42 procedure SaveWindowstateEx(ini: TRegInifile; form: TForm; const section: string);
43 var
44 wp: TWindowPlacement;
45 begin
46 wp.length := Sizeof(wp);
47 GetWindowPlacement(form.handle, @wp);
48 with Ini, wp.rcNormalPosition do
49 begin
50 WriteInteger(section, sLeft, Left);
51 WriteInteger(section, sTop, Top);
52 WriteInteger(section, sWidth, Right - Left);
53 WriteInteger(section, sHeight, Bottom - Top);
54 WriteString(section, sState, GetEnumName(TypeInfo(TWindowState),
55 Ord(form.WindowState));
56 end;
57 end;
58
59 {Procedure RestoreWindowStateEx;
60
61 Parameters:
62 ini: inifile to restore the settings from
63 form: form to restore the settings for
64 section: section to use for the settings
65
66 Call method: static
67
68 Description:
69 Restores the window position and dimensions from the saved values in the INI file.
70 If there ain't any, nothing changes.
71
72 Error Conditions: none
73
74 Created: 03.07.97 16:33:27 by P. Below}
75
76 procedure RestoreWindowstateEx(ini: TRegInifile; form: TForm; const section:
77 string);
78 var
79 L, T, W, H: Integer;
80 begin
81 with Ini, form do
82 begin
83 L := ReadInteger(section, sLeft, Left);
84 T := ReadInteger(section, sTop, Top);
85 W := ReadInteger(section, sWidth, Width);
86 H := ReadInteger(section, sHeight, Height);
87 SetBounds(L, T, W, H);
88 try
89 Windowstate := TWindowState(GetEnumValue(TypeInfo(TWindowState),
90 ReadString(section, sState, 'wsNormal')));
91 except
92 end;
93 end;
94 end;
95
96 { Save state using default settings section }
97
98 procedure SaveWindowstate(ini: TRegInifile; form: TForm);
99 begin
100 SaveWindowstateEx(ini, form, sSettings);
101 end;
102
103 { Restore state using default settings section }
104
105 procedure RestoreWindowstate(ini: TRegInifile; form: TForm);
106 begin
107 RestoreWindowStateEx(ini, form, sSettings);
108 end;
109
110 end.
|