| 1 | unit UPersistentForm;
|
|---|
| 2 |
|
|---|
| 3 | {$mode delphi}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | Classes, SysUtils, Forms, URegistry, LCLIntf, Registry;
|
|---|
| 9 |
|
|---|
| 10 | type
|
|---|
| 11 |
|
|---|
| 12 | { TPersistentForm }
|
|---|
| 13 |
|
|---|
| 14 | TPersistentForm = class
|
|---|
| 15 | public
|
|---|
| 16 | RegistryKey: string;
|
|---|
| 17 | RegistryRootKey: HKEY;
|
|---|
| 18 | procedure Load(Form: TForm);
|
|---|
| 19 | procedure Save(Form: TForm);
|
|---|
| 20 | constructor Create;
|
|---|
| 21 | end;
|
|---|
| 22 |
|
|---|
| 23 | implementation
|
|---|
| 24 |
|
|---|
| 25 | { TPersistentForm }
|
|---|
| 26 |
|
|---|
| 27 | procedure TPersistentForm.Load(Form: TForm);
|
|---|
| 28 | var
|
|---|
| 29 | RestoredLeft, RestoredTop, RestoredWidth, RestoredHeight: Integer;
|
|---|
| 30 | begin
|
|---|
| 31 | with TRegistryEx.Create do
|
|---|
| 32 | try
|
|---|
| 33 | RootKey := RegistryRootKey;
|
|---|
| 34 | OpenKey(RegistryKey + '\Forms\' + Form.Name, True);
|
|---|
| 35 |
|
|---|
| 36 | Form.Width := ReadIntegerWithDefault('Width', Form.Width);
|
|---|
| 37 | Form.Height := ReadIntegerWithDefault('Height', Form.Height);
|
|---|
| 38 | Form.Top := ReadIntegerWithDefault('Top', (Screen.Height - Form.Height) div 2);
|
|---|
| 39 | Form.Left := ReadIntegerWithDefault('Left', (Screen.Width - Form.Width) div 2);
|
|---|
| 40 | if Form.Left < 0 then
|
|---|
| 41 | Form.Left := 0;
|
|---|
| 42 | if Form.Left > (Screen.Width - 50) then
|
|---|
| 43 | Form.Left := Screen.Width - 50;
|
|---|
| 44 | if Form.Top < 0 then
|
|---|
| 45 | Form.Top := 0;
|
|---|
| 46 | if Form.Top > (Screen.Height - 50) then
|
|---|
| 47 | Form.Top := Screen.Height - 50;
|
|---|
| 48 | RestoredWidth := ReadIntegerWithDefault('RestoredWidth', Form.RestoredWidth);
|
|---|
| 49 | RestoredHeight := ReadIntegerWithDefault ('RestoredHeight', Form.RestoredHeight);
|
|---|
| 50 | RestoredTop := ReadIntegerWithDefault ('RestoredTop', (Screen.Height - Form.RestoredHeight) div 2);
|
|---|
| 51 | RestoredLeft := ReadIntegerWithDefault ('RestoredLeft', (Screen.Width - Form.RestoredWidth) div 2);
|
|---|
| 52 | Form.SetRestoredBounds(RestoredLeft, RestoredTop, RestoredWidth, RestoredHeight);
|
|---|
| 53 |
|
|---|
| 54 | Form.WindowState := TWindowState(ReadIntegerWithDefault('WindowState', Integer(wsNormal)));
|
|---|
| 55 | if ReadBoolWithDefault('Visible', False) then Form.Show;
|
|---|
| 56 | finally
|
|---|
| 57 | Free;
|
|---|
| 58 | end;
|
|---|
| 59 | end;
|
|---|
| 60 |
|
|---|
| 61 | procedure TPersistentForm.Save(Form: TForm);
|
|---|
| 62 | begin
|
|---|
| 63 | with Form, TRegistryEx.Create do
|
|---|
| 64 | try
|
|---|
| 65 | RootKey := RegistryRootKey;
|
|---|
| 66 | OpenKey(RegistryKey + '\Forms\' + Form.Name, True);
|
|---|
| 67 | WriteInteger('Width', Form.Width);
|
|---|
| 68 | WriteInteger('Height', Form.Height);
|
|---|
| 69 | WriteInteger('Top', Form.Top);
|
|---|
| 70 | WriteInteger('Left', Form.Left);
|
|---|
| 71 | WriteInteger('RestoredWidth', Form.RestoredWidth);
|
|---|
| 72 | WriteInteger('RestoredHeight', Form.RestoredHeight);
|
|---|
| 73 | WriteInteger('RestoredTop', Form.RestoredTop);
|
|---|
| 74 | WriteInteger('RestoredLeft', Form.RestoredLeft);
|
|---|
| 75 | WriteInteger('WindowState', Integer(Form.WindowState));
|
|---|
| 76 | WriteBool('Visible', Form.Visible);
|
|---|
| 77 | finally
|
|---|
| 78 | Free;
|
|---|
| 79 | end;
|
|---|
| 80 | end;
|
|---|
| 81 |
|
|---|
| 82 | constructor TPersistentForm.Create;
|
|---|
| 83 | begin
|
|---|
| 84 | RegistryRootKey := HKEY_CURRENT_USER;
|
|---|
| 85 | end;
|
|---|
| 86 |
|
|---|
| 87 | end.
|
|---|
| 88 |
|
|---|