Title: Save and Restore Size and Position of your form
Question: Save and Restore Size and Position of your form using the Registry. For ease of testing I have enclosed a way to easily restart the application by clicking on the form anywhere, to make this happen I had to add Shellapi to the uses clausule. And to create this version the Registry was added to the users clausule
Answer:
Fixed a bug!
There is a different version of this project which makes use of an inifile instead, for ease of reading I created an additional download for it.
There are many more things possible with Delphi than one can make up from the poor help files that accompany it. There is virtually no conceptual help available from the standard help file, and that is just what can be so helpful for programming! I always try to not use any 3rd party component, so I can publish my stuff where and how I like it. Do not use 3rd party components if you do not have to, apart from the fact that doing it yourself gives much more satisfaction!
Save and Restore Size and Position of your form using the registry. For ease of testing I have enclosed a way to easily restart the application by clicking on the form anywhere, to make this happen I had to add Shellapi to the uses clausule. And to create this version the Registry was added to the users clausule
------------------------------------------------------------------
Project1.dpr
------------------------------------------------------------------
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
------------------------------------------------------------------
------------------------------------------------------------------
Unit1.dfm
------------------------------------------------------------------
object Form1: TForm1
Left = 198
Top = 116
Width = 400
Height = 480
Caption = 'SRForm Size and Position using Reg'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnClick = FormClick
OnClose = FormClose
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object lbl1: TLabel
Left = 130
Top = 213
Width = 133
Height = 26
Caption = 'Click to test'#13#10'[application will restart]'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = [fsBold]
ParentFont = False
end
end
------------------------------------------------------------------
------------------------------------------------------------------
Unit1.pas
------------------------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Registry, Shellapi, StdCtrls;
type
TForm1 = class(TForm)
lbl1: TLabel;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure FormClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure ReadFromRegistry;
var
R : TRegistry;
begin
R := TRegistry.Create;
try
R.RootKey := HKEY_CURRENT_USER;
if R.OpenKey('\Software\fdehell\srf', True) then
begin
Form1.left:=R.ReadInteger('left');
Form1.top:=R.ReadInteger('top');
Form1.height:=R.ReadInteger('height');
Form1.width:=R.ReadInteger('width');
end;
Finally
R.CloseKey;
R.Free;
end;
end;
procedure WriteToRegistry;
var
R : TRegistry;
begin
R := TRegistry.Create;
try
R.RootKey := HKEY_CURRENT_USER;
if R.OpenKey('\Software\fdehell\srf', True) then
R.WriteInteger('left',Form1.Left);
R.WriteInteger('top',Form1.Top);
R.WriteInteger('height', Form1.Height);
R.WriteInteger('width', Form1.Width);
Finally
R.CloseKey;
R.Free;
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
R : TRegistry;
begin
R := TRegistry.Create;
try
R.RootKey := HKEY_CURRENT_USER;
if R.OpenKey('\Software\fdehell\srf', True) then
R.WriteInteger('left',Form1.Left);
R.WriteInteger('top',Form1.Top);
R.WriteInteger('height', Form1.Height);
R.WriteInteger('width', Form1.Width);
Finally
R.CloseKey;
R.Free;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ReadFromRegistry;
end;
procedure TForm1.FormClick(Sender: TObject);
var
AppName : PChar;
begin
AppName := PChar( Application.ExeName );
ShellExecute( Handle, 'open', AppName, nil, nil, SW_SHOWNORMAL );
close;
end;
end.