unit savepos; { A component for saving and restoring the last position of the form. Author: Vesa Lappalainen / Kave OY Date: 1996 Changes: 28.2.1998 + Changed to use generic TIniSave-component Changes: 26.7.1998 + NoPos, NoSize -attributes Changed : 15.9.2001/vl + VCL/CLX compiling (define const CLX) Usage: Just drop the component to the form and the position and size of the form is saved to the .ini -file when program ends and restored when program starts. } interface uses SysUtils, Classes, {$ifdef CLX} QGraphics, QControls, QForms, QDialogs, {$else} Windows, Messages, Graphics, Controls, Forms, Dialogs, {$endif} kIniSave; type TSavePos = class(TComponent) private Form : TForm; FIni : TIniSave; FNoPos: boolean; FNoSize: boolean; FEnabled: boolean; procedure SetEnabled(const Value: boolean); protected public constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure Loaded; override; function GetAsString : string; virtual; procedure SetAsString(s:string); virtual; // function GetIniSection : string; virtual; // procedure SetIniSection(value:string); virtual; // procedure SetNoPos(b:boolean); virtual; published property AsString : string read GetAsString write SetAsString stored false; property Ini : TIniSave read FIni write FIni; property NoPos : boolean read FNoPos write FNoPos; property NoSize : boolean read FNoSize write FNoSize; property Enabled : boolean read FEnabled write SetEnabled; // property IniSection : string read GetIniSection write SetIniSection; end; procedure ApplyToForm(Form:TForm;s:string;NoPos:boolean=false;NoSize:boolean=false); function GetFormPos(Form:TForm):string; procedure Register; implementation uses kDouble; //------------------------------------------------------------------------------ procedure Register; begin RegisterComponents('KaveOptions', [TSavePos]); end; //------------------------------------------------------------------------------ procedure TSavePos.Loaded; var f:TForm; begin inherited; Form := NIL; if ( csDesigning in ComponentState ) then exit; if not ( Owner is TForm ) then exit; f := Owner as TForm; Form := f; if ( Ini.SaveName = '' ) then Ini.SaveName := f.Name; Ini.Load; end; //------------------------------------------------------------------------------ constructor TSavePos.Create(AOwner: TComponent); begin inherited Create(AOwner); FIni := TIniSave.Create(self); FNoPos := false; FNoSize := false; FEnabled := true; Ini.AutoSave := true; Ini.PropName := 'AsString'; Ini.IniSection:= 'Pos'; end; //------------------------------------------------------------------------------ destructor TSavePos.Destroy; begin if ( Enabled ) then Ini.Save; Ini.Free; inherited Destroy; end; //----------------------------------------------------------------------------- procedure ApplyToForm(Form:TForm;s:string;NoPos,NoSize:boolean); begin if ( Form = NIL ) then exit; if ( not NoPos ) then begin Form.Left := ExtractInt(s,',',Form.Left); Form.Top := ExtractInt(s,',',Form.Top); end else begin ExtractInt(s,',',Form.Left); ExtractInt(s,',',Form.Top); end; if ( not NoSize ) then begin Form.Width := ExtractInt(s,',',Form.Width); Form.Height := ExtractInt(s,',',Form.Height); end; if ( Form.Left >= Screen.Width ) then Form.Left := 0; if ( Form.Top >= Screen.Height ) then Form.Top := 0; if ( Form.Left + Form.Width <= 0 ) then Form.Left := 0; if ( Form.Top + Form.Height <= 0 ) then Form.Top := 0; end; //----------------------------------------------------------------------------- procedure TSavePos.SetAsString(s:string); // virtual; begin if ( csDesigning in ComponentState ) then exit; ApplyToForm(Form,s,NoPos,NoSize); end; //----------------------------------------------------------------------------- function GetFormPos(Form:TForm):string; begin Result := ''; if ( Form = NIL ) then exit; Result := IntToStr(Form.Left) + ',' + IntToStr(Form.Top) + ',' + IntToStr(Form.Width) + ',' + IntToStr(Form.Height); end; //----------------------------------------------------------------------------- function TSavePos.GetAsString : string; // virtual; begin Result := ''; if ( csDesigning in ComponentState ) then exit; Result := GetFormPos(Form); end; (* //----------------------------------------------------------------------------- procedure TSavePos.SetIniSection(value:string); // virtual; begin value := '';; end; //----------------------------------------------------------------------------- function TSavePos.GetIniSection : string; // virtual; begin Result := Ini.IniSection; end; *) procedure TSavePos.SetEnabled(const Value: boolean); begin FEnabled := Value; end; end.