Translate to:

Online Delphi Training

Learn LIVE
From The Comfort Of Your Home
Start Immediately!

+1 (678) 921-0644


How To Save A Window's Size & Position In Delphi

I sometimes get asked the question: How do you save the position and size of a given window based on a user's preference, and then use the saved settings next time you open the window?

The solution to this problem is quite simple. First, you must decide how to save the settings. The three most common ways are:

  1. Use an INI File - Typically one user per computer
  2. Use the Windows Registry - Can be for a single user, or for multiple users with different accounts sharing the same computer
  3. Use a database table - Client/Server environmnet where one user can use any networked computer

I will only cover the first method - the use of an INI file to save the settings. The concept is the same for all three:

When the window is opened, check to see if you should load any saved settings and if they had been saved. If so, use the settings to display the window. Otherwise, load the default window settings (design settings). Before closing the window, check to see if you should save any settings. If so, save the settings and exit. You can tweak and customize the code to your specifications and liking, and modify it to suit your choice of storing methodology.

For this example, we'll use the main form (Window), drop a panel onto it and a check-box onto the panel:

Save size & position example in Delphi

To keep the panel always centered in the window, add the following code to the Form's OnResize Event:  

myPanel.Top := frmMain.ClientHeight div 2 -  myPanel.Height div 2;
myPanel.Left := frmMain.ClientWidth div 2 -  myPanel.Width div 2;

In the private declarations of the interface section, add:

myINIFile : TINIFile;

The above line declares myINIFile as an INI file and makes it available to all procedures in this unit.

Add "INIFiles" to the USES section under INTERFACE.

Following is the commented code for the Form's OnCreate Event:

procedure TfrmMain.FormCreate(Sender: TObject);
var
   myFileName : String;    // This will hold the full path and name of our INI file for easy reference
   SaveSettings : Integer;   // If the value of this variable is '1', the settings are saved
begin
   // The next line sets the full path and name of our INI file based on the location of the executable.
   myFileName := ExtractFilePath( Application.ExeName ) +  'FormSettings.ini';

   myINIFile := TINIFile.Create(myFileName); // Initializes the INI file and makes it available for use

   // We will use a try-finally-end block to ensure the proper handling of resources
   try
      // The function ReadInteger is used to read an integer from the INI file. It takes three
      // parameters. The first one is the Section of the INI file. The second is the name of the key
      // from which the value is retrieved. The third parameter is the integer value that should be
      // returned if: (1) The Section does not exist; (2) The Key does not exist; (3) The Key does not
      // have an assigned value.  

      SaveSettings := myINIFile.ReadInteger('Preferences', 'SaveSettings', -1);
      If SaveSettings = 1 then
         Begin
            cbSave.Checked := True;
            With myINIFile do
                  begin
                        self.Top := ReadInteger('MainForm', 'Top', -1);
                        self.Left := ReadInteger('MainForm', 'Left', -1);
                        self.Height := ReadInteger('MainForm', 'Height', -1);
                        self.Width := ReadInteger('MainForm', 'Width', -1);
                   end;
         End
      Else cbSave.Checked := False;
   finally
      myINIFile.Free;  // Frees the resources of the INI file
   end;
end;

Following is the commented code for the Form's OnDestroy Event:

procedure TfrmMain.FormDestroy(Sender: TObject);
var
   myFileName : String;    // This will hold the full path and name of our INI file for easy reference
begin
   // The next line sets the full path and name of our INI file based on the location of the executable.
   myFileName := ExtractFilePath( Application.ExeName ) +  'FormSettings.ini';

   myINIFile := TINIFile.Create(myFileName); // Initializes the INI file and makes it available for use

   // We will use a try-finally-end block to ensure the proper handling of resources
   try
      // The function WriteInteger is used to write an integer value to a key in the INI file. It takes three
      // parameters. The first one is the Section of the INI file. The second is the name of the key
      // to which the value is written. The third parameter is the actual integer value.  

      With myINIFile do
         Begin
            If cbSave.Checked then
                  begin
                        WriteInteger('Preferences', 'SaveSettings', 1);
                        WriteInteger('MainForm', 'Top', self.Top);
                        WriteInteger('MainForm', 'Left', self.Left);
                        WriteInteger('MainForm', 'Height', self.Height);
                        WriteInteger('MainForm', 'Width', self.Width);
                   end
            else WriteInteger('Preferences', 'SaveSettings', 0);
         End;
   finally
      myINIFile.Free;
   end;
end;



Do you have any comments about this topic?

Click on: Post Comments/Questions

To download the project click on: Delphi Tip Download


©2005-2006 Business-IQ