Answer:
Updated, fixed a bug!
There is a different version of this project which makes use of the registry 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 an Inifile. This form remembers the size of its window and the position on the screen, so when you restart it it restores the last width, height and x y location when you closed it. A very simple form enhancement, which you can easily write yourself!
------------------------------------------------------------------
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 = 258
Top = 172
Width = 400
Height = 480
Caption = 'SRForm Size and Position using Ini'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnClick = FormClick
OnCloseQuery = FormCloseQuery
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
Layout = tlCenter
end
end
------------------------------------------------------------------
------------------------------------------------------------------
Unit1.pas
------------------------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IniFiles, StdCtrls, ShellAPI;
type
TForm1 = class(TForm)
lbl1: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure FormClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
F: TextFile;
Value: Integer;
begin
if FileExists(ChangeFileExt(ParamStr(0), '.ini')) then
begin
AssignFile(F, ChangeFileExt(ParamStr(0), '.ini'));
Reset(F);
Readln(F, Value);
Left:= Value;
Readln(F, Value);
Top:= Value;
Readln(F, Value);
Width:= Value;
Readln(F, Value);
Height:= Value;
Readln(F, Value);
WindowState:= TWindowState(Value);
CloseFile(F);
end;
end;
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var
F: TextFile;
Value: Integer;
begin
AssignFile(F, ChangeFileExt(ParamStr(0), '.ini'));
if (WindowState wsNormal) and (FileExists(ChangeFileExt(ParamStr(0), '.ini')))
then
begin
Reset(F);
Readln(F, Value);
Left:= Value;
Readln(F, Value);
Top:= Value;
Readln(F, Value);
Width:= Value;
Readln(F, Value);
Height:= Value;
CloseFile(F);
end;
Rewrite(F);
Writeln(F, Left);
Writeln(F, Top);
Writeln(F, Width);
Writeln(F, Height);
Writeln(F, Byte(WindowState));
CloseFile(F);
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.
------------------------------------------------------------------
|