1const
2 CRegKey = 'Software\Demos\WdwStateDemo\1.0';
3
4
5
6function ReadIntFromReg(Reg: TRegistry;
7 Name: string; Def: Integer): Integer;
8
9
10
11begin
12 if Reg.ValueExists(Name) then
13 Result := Reg.ReadInteger(Name)
14 else
15 Result := Def;
16end;
17
18procedure TForm1.FormDestroy(Sender: TObject);
19var
20 Reg: TRegistry;
21 State: Integer;
22 Pl : TWindowPlacement;
23 R: TRect;
24begin
25
26
27
28
29
30 Pl.Length := SizeOf(TWindowPlacement);
31 GetWindowPlacement(Self.Handle, @Pl);
32 R := Pl.rcNormalPosition;
33 Reg := TRegistry.Create;
34 try
35
36 Reg.RootKey := HKEY_CURRENT_USER;
37 Reg.OpenKey(CRegKey, True);
38
39 Reg.WriteInteger('Width', R.Right-R.Left);
40 Reg.WriteInteger('Height', R.Bottom-R.Top);
41 Reg.WriteInteger('Left', R.Left);
42 Reg.WriteInteger('Top', R.Top);
43
44
45
46
47
48
49
50 if IsIconic(Application.Handle) then
51
52 State := Ord(wsMinimized)
53 else
54
55
56 State := Ord(Self.WindowState);
57 Reg.WriteInteger('State', State);
58 finally
59 Reg.Free;
60 end;
61end;
62
63procedure TForm1.FormCreate(Sender: TObject);
64var
65 Reg: TRegistry;
66 State: Integer;
67begin
68 Reg := TRegistry.Create;
69 try
70
71 Reg.RootKey := HKEY_CURRENT_USER;
72 if not Reg.OpenKey(CRegKey, False) then Exit;
73
74
75 Self.Width := ReadIntFromReg(Reg, 'Width', Self.Width);
76 Self.Height := ReadIntFromReg(Reg, 'Height', Self.Height);
77 Self.Left := ReadIntFromReg(Reg, 'Left', Self.Left);
78 Self.Top := ReadIntFromReg(Reg, 'Top', Self.Top);
79
80 State := ReadIntFromReg(Reg, 'State', Ord(wsNormal));
81
82
83
84
85 if State = Ord(wsMinimized) then
86 begin
87
88
89
90
91 Self.Visible := True;
92 Application.Minimize;
93 end
94 else
95 Self.WindowState := TWindowState(State);
96 finally
97 Reg.Free;
98 end;
99end;