Forms Delphi

unit Unit1;
{(*******************************************************************************
DynaForm Form Template for dynamic forms.
© 1995 Objective Software Technology Pty Ltd.
Publishers of ABC for Delphi and other fine Delphi tools.
Email 100035.2441@compuserve.com
Fax +61 6 2732190
Phone +61 6 2732100
********************************************************************************
Purpose:
This template allows AUTOMATIC configuration of user changes to forms,
without needing any additional code to save/restore changed properties.
}
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;
type
TDynaForm = class(TForm)
Label1: TLabel;
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button2: TButton;
Button3: TButton;
Button4: TButton;
ColorDialog1: TColorDialog;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
FResFile: TFileName;
FNoRestore: Boolean;
FNoSave: Boolean;
FOnRestore: TNotifyEvent;
FOnSave: TNotifyEvent;
procedure SaveToFile(AResFile: TFileName);
procedure RestoreFromFile(AResFile: TFileName);
function GetDefaultResFile: TFileName;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
{ The following public properties can be set in code to change the filing
behaviour - for example in OnCreate}
property ResFile: TFileName read FResFile write FResFile; {also defaults to the constant declared below}
property NoRestore: Boolean read FNoRestore write FNoRestore; {must be set in OnCreate event}
property NoSave: Boolean read FNoSave write FNoSave;
property OnRestore: TNotifyEvent read FOnRestore write FOnRestore; {must be set in OnCreate event}
property OnSave: TNotifyEvent read FOnSave write FOnSave;
end;
var
DynaForm: TDynaForm;
implementation
{$R *.DFM}
const
{
Edit this constant to set a static file name for instances of this form.
e.g. UNIT1.DFR
If no name is specified at runtime, the template will
Create/Read/Write a file of this name in the exe directory
}
DefaultResFile = 'UNIT1.DFR';
constructor TDynaForm.Create(AOwner: TComponent);
{
Create form instance from EXE and then restore saved property values.
First does inherited Form Create and OnCreate event;
Set the NoRestore property to True in OnCreate to disable these functions:
- rereads the published form and component properties from a Res File;
- executes the OnRestore event if assigned.
}
begin
inherited Create(AOwner);
if not NoRestore then begin
if ResFile = '' then ResFile := GetDefaultResFile;
if ResFile <> '' then begin
RestoreFromFile(ResFile);
if Assigned(FOnRestore) then FOnRestore(self);
end;
end;
end;
destructor TDynaForm.Destroy;
{
Save the published form and component properties to a Res File
before destroying.
Set NoSave to true to disable this function.
}
begin
if Assigned(FOnSave) then FOnSave(self);
if not NoSave then begin
if ResFile = '' then ResFile := GetDefaultResFile;
if ResFile <> '' then SaveToFile(ResFile);
end;
inherited Destroy;
end;
procedure TDynaForm.SaveToFile(AResFile: TFileName);
begin
WriteComponentResFile(AResFile, Self);
end;
procedure TDynaForm.RestoreFromFile(AResFile: TFileName);
var
Stream: TFileStream;
I: integer;
begin
try
Stream := TFileStream.Create(AResFile, fmOpenRead);
try
{delete all components}
for I := ComponentCount - 1 downto 0 do
Components[I].Free;
Stream.ReadComponentRes(Self);
finally
Stream.Free;
end;
except
on EFOpenError do {nothing};
end;
end;
function TDynaForm.GetDefaultResFile: TFileName;
begin
if DefaultResFile = '' then begin
ShowMessage('DefaultResFile constant not defined');
Result := '';
end
else
Result := ExtractFilePath(Application.ExeName) + DefaultResFile;
end;
procedure TDynaForm.Button1Click(Sender: TObject);
var
I: integer;
begin
ColorDialog1.Color := Edit1.Color;
if ColorDialog1.Execute then
for I := 0 to ComponentCount - 1 do
if Components[I] is TEdit then
TEdit(Components[I]).Color := ColorDialog1.Color;
end;
procedure TDynaForm.Button2Click(Sender: TObject);
var
NewPos, I: integer;
begin
NewPos := Edit1.Left - 10;
for I := 0 to ComponentCount - 1 do
if Components[I] is TEdit then
TEdit(Components[I]).Left := NewPos;
end;
procedure TDynaForm.Button3Click(Sender: TObject);
var
NewPos, I: integer;
begin
NewPos := Edit1.Left + 10;
for I := 0 to ComponentCount - 1 do
if Components[I] is TEdit then
TEdit(Components[I]).Left := NewPos;
end;
procedure TDynaForm.Button4Click(Sender: TObject);
var
LastTop, I: integer;
NewEdit: TEdit;
begin
LastTop := 0;
for I := 0 to ComponentCount - 1 do
if Components[I] is TEdit then
if LastTop < TEdit(Components[I]).Top then
LastTop := TEdit(Components[I]).Top;
NewEdit := TEdit.Create(self);
with NewEdit do begin
Parent := DynaForm;
Top := LastTop + 32;
Left := Edit1.Left;
Color := Edit1.Color;
end;
end;
end.