📄 usettingskeeper.pas
字号:
Ini := TIniFile.Create(FIniFileName); //open the ini file
try
for i := 0 to FSettings.Count - 1 do //save all settings
TAbstractSettings(FSettings.Objects[i]).SaveToIni(Ini);
finally
Ini.Free; //close the ini file
end;
end;
{Registers an object of settings to be kept. (visibility: file scope!)
~param Settings the settings to register }
procedure TSettingsKeeper.RegisterSettings(Settings: TAbstractSettings);
begin
Assert(FSettings.IndexOf(Settings.Name) = -1);
FSettings.AddObject(Settings.Name, Settings); //add the settings to the list
end;
{Initializes settings from the ini file. (visibility: file scope!)
The default values of the settings should probably be set afore.
~param Settings the settings to initialize from the ini file }
procedure TSettingsKeeper.Initialize(Settings: TAbstractSettings);
begin
Assert(FSettings.IndexOf(Settings.Name) <> -1);
Assert(FSettings.Objects[FSettings.IndexOf(Settings.Name)] = Settings);
Settings.LoadFromIni(FInitialization); //load settings from the ini file
end;
{Returns the kept object of the settings or nil. (visibility: file scope!)
~param Name the name of the settings to return
~result the kept object of the settings or nil }
function TSettingsKeeper.GetSettings(const Name: String): TAbstractSettings;
var i :Integer; //index of the settings
begin
assert(assigned(FInitialization));
i := FSettings.IndexOf(Name); //search the settings
if i <> -1 then //settings found?
Result := TAbstractSettings(FSettings.Objects[i]) //return the settings
else
Result := nil; //settings not found
end;
{Sets the name of the ini file to read from and write to and reads its content.
~param IniFileName the name of the ini file to read and save the settings
from/to }
class procedure TSettingsKeeper.SetIniFileName(const IniFileName: String);
begin
SettingsKeeper.InternalSetIniFileName(IniFileName); //forward to the object
end;
{Saves all settings to the set ini file. }
class procedure TSettingsKeeper.SaveSettings;
begin
SettingsKeeper.InternalSaveSettings; //forward to the object
end;
{ * * * *** * * * *** TAbstractSettings *** * * * *** * * * }
{Creates the object for settings and saves its associated name.
~param Name the name of the settings }
constructor TAbstractSettings.Create(const Name: String);
begin
inherited Create; //create the object
FName := Name; //save the name
SettingsKeeper.RegisterSettings(Self); //register the settings
end;
{Returns the kept object of the settings with the associated name or nil.
~param Name the name of the settings to return
~result the kept object of the settings or nil if not found }
class function TAbstractSettings.GetSettings(const Name: String):
TAbstractSettings;
begin
Result := SettingsKeeper.GetSettings(Name); //get the object from the keeper
Assert(not Assigned(Result) or (Result.ClassType = Self));
end;
{Initializes the settings from the main ini file. Before this method is called
the settings should probably be initialized with their default values so they
can be used when reading values from the ini file via ~[link LoadFromIni] that
aren't set. }
procedure TAbstractSettings.Initialize;
begin
SettingsKeeper.Initialize(Self); //let the keeper initialize itself
end;
{ * * * *** * * * *** TFormSettings *** * * * *** * * * }
{Decides what settings of the form have to be saved.
~param Form the form whose settings to save }
procedure TFormSettings.ReadWhatToSave(Form: TForm);
begin
//only save the size of the form if it is sizable
FSaveFormSize := Form.BorderStyle in
{$IFNDEF LINUX}
[bsSizeable, bsSizeToolWin];
{$ELSE}
[fbsSizeable, fbsSizeToolWin];
{$ENDIF}
//only save state of the form if is sizable and can be maximized or minimized
FSaveFormState := (Form.BorderStyle =
{$IFNDEF LINUX}
bsSizeable
{$ELSE}
fbsSizeable
{$ENDIF}
) and (Form.BorderIcons * [biMinimize, biMaximize] <> []);
end;
{Saves the basic form values in settings by the name of its class.
~param Form the form whose settings to save }
class procedure TFormSettings.SaveBasicFormValues(Form: TForm);
var Settings :TFormSettings; //to read ini settings of the form
begin
Assert(Self = TFormSettings);
//get object to load the settings from
Settings := TFormSettings(TFormSettings.GetSettings(Form.ClassName));
if not Assigned(Settings) then //no object initialized?
begin //create a new object
Settings := TFormSettings.Create(Form.ClassName);
Settings.GetValuesFromForm(Form); //initialize with the default values
Settings.Initialize; //and read from the ini file
end;
Settings.SetValuesToForm(Form); //initialize form with read values
end;
{Loads the settings from the ini file.
~param Ini the ini file to load the settings from }
procedure TFormSettings.LoadFromIni(Ini: TCustomIniFile);
var i :Integer; //read values from the ini file
begin
i := Ini.ReadInteger(Name, 'Left', FLeft);
if i >= 0 then //if X position valid, set it
FLeft := i;
i := Ini.ReadInteger(Name, 'Top', FTop);
if i >= 0 then //if Y position valid, set it
FTop := i;
i := Ini.ReadInteger(Name, 'ClientWidth', FWidth);
if i >= 0 then //if width valid, set it
FWidth := i;
i := Ini.ReadInteger(Name, 'ClientHeight', FHeight);
if i >= 0 then //if height valid, set it
FHeight := i;
//read the window state and set it if valid
i := Ini.ReadInteger(Name, 'WindowState', Ord(FWindowState));
if (i >= Ord(Low(TWindowState))) and (i >= Ord(High(TWindowState))) then
FWindowState := TWindowState(i);
end;
{Saves the settings to the ini file.
~param Ini the ini file to save the settings to }
procedure TFormSettings.SaveToIni(Ini: TCustomIniFile);
begin
//window is not maximized?
if not FSaveFormState or (FWindowState <> wsMaximized) then
begin //write position
Ini.WriteInteger(Name, 'Left', FLeft);
Ini.WriteInteger(Name, 'Top', FTop);
if FSaveFormSize then //size should be written?
begin
Ini.WriteInteger(Name, 'ClientWidth', FWidth);
Ini.WriteInteger(Name, 'ClientHeight', FHeight);
end;
end;
if FSaveFormState then //state should be written?
//write the state of the window
Ini.WriteInteger(Name, 'WindowState', ord(FWindowState));
end;
{Gets the settings from the form.
~param Form the form to update the settings from }
procedure TFormSettings.GetValuesFromForm(Form: TForm);
begin
ReadWhatToSave(Form); //decide what has to be saved
FWindowState := Form.WindowState;
if not FSaveFormState or (FWindowState <> wsMaximized) then
begin
FLeft := Form.Left; //get the state of the form
FTop := Form.Top;
FWidth := Form.ClientWidth;
FHeight := Form.ClientHeight;
end;
end;
{Assigns the settings to the form. It will also set whether the hints should be
shown depending on whether they are shown on the main form.
~param Form the form to assign the settings to }
procedure TFormSettings.SetValuesToForm(Form: TForm);
begin
ReadWhatToSave(Form); //decide what has to be saved
if not FSaveFormState or (FWindowState <> wsMaximized) then
begin
if (FLeft >= 0) and (FLeft < Screen.Width - 10) then
Form.Left := FLeft; //assign the position
if (FTop >= 0) and (FTop < Screen.Height - 10) then
Form.Top := FTop;
if FSaveFormSize then //the size of the form can be changed?
begin
if FWidth > 10 then
Form.ClientWidth := FWidth; //assign the size
if FHeight > 10 then
Form.ClientHeight := FHeight;
end;
end;
if FSaveFormState then //the state of the form can be changed?
Form.WindowState := FWindowState; //assign the state
//inherit from the main form whether the hints should be shown
if Assigned(Application.MainForm) and (Form <> Application.MainForm) then
Form.ShowHint := Application.MainForm.ShowHint;
// Form.Font.Name := 'Verdana';
// Form.Font.Name := 'Tahoma';
end;
initialization
//create the one and only keeper of settings
SettingsKeeper := TSettingsKeeper.Create;
finalization
//free the one and only keeper of settings
SettingsKeeper.Free;
// SettingsKeeper := nil;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -