Title: Saving form size
Question: Ever tried to deploy an application on mulitule resulotion screens ?
Every user tends to like the forms to be shaped according to his own preferences. Using a simple drop-on component solves this problem.
Answer:
Dropping this comoponent on the form redirects the WindowProc so that the component saves the size of the form in an ini file every time the form is closed. It is easy to modify this code to save various other parameters as well.
unit SizeSaver;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, IniFiles;
type
TSizeSaver = class(TComponent)
private
{ Private declarations }
OldWindProc : TWndMethod;
ShowFlag : boolean;
procedure Over(var Message: TMessage);
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner : TComponent);override;
published
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TSizeSaver]);
end;
{ TSizeSaver }
constructor TSizeSaver.Create(AOwner: TComponent);
begin
inherited;
ShowFlag := False;
if (AOwner is TControl) then
with (Aowner as TControl) do
begin
if not (csDesigning in ComponentState) then
begin
OldWindProc := WindowProc;
WindowProc := Over;
end
end;
end;
procedure TSizeSaver.Over(var Message: TMessage);
var
i : integer;
IniFile : TInifile;
begin
case Message.Msg of
WM_CLOSE :
begin
IniFile := TIniFile.Create(Owner.Name+'.ini');
for i:=0 to (Application.ComponentCount-1) do
if Application.Components[i] is TForm then
with (Application.Components[i] as TControl) do
begin
IniFile.WriteString(Name,'Height',IntToStr(Height));
IniFile.WriteString(Name,'Width',IntToStr(Width));
end;
IniFile.Free;
end;
WM_SHOWWINDOW :
if (not ShowFlag) then
begin
IniFile := TIniFile.Create(Owner.Name+'.ini');
for i:=0 to (Application.ComponentCount-1) do
if Application.Components[i] is TForm then
with (Application.Components[i] as TControl) do
begin
Height := StrToInt(IniFile.ReadString(Name,'Height','300'));
Width := StrToInt(IniFile.ReadString(Name,'Width','300'));
end;
IniFile.Free;
ShowFlag := True;
end
end;
OldWindProc(Message);
end;
end.