Saving Forms' Position & Size (and a handy tip from my wife)
written by Rick Spence
BACK to TIPS n TRICKS
Before I get into this one, a quick tip from my wife. How do you know when your application is crashed? "Keep pressing keys until the keyboard beeps". That said, let's dive into this quick tip.
Users like to resize your application's forms and reposition them. It's easy to implement your forms so they remember where they were last displayed, and how big they were. This way, the next time you display the form you can position and size it the same way.
To implement this, you need to store the following information somewhere:
1. The form's top and left properties
2. The form's height and width properties
Since this needs to be stored for each user, the best solution is either an INI file or the registry, on each user's machine. This way, each user maintains his / her own settings. I must confess to still using INI files for most of these type of settings. I say confess, because most people recommend using the registry and I always feel a little guilty for sticking with INI files. So why do I continue to use them?
1. I can look at them and change them with a simple text editor
2. I can email them to my users
3. My users can email them to me
4. I once learnt to use the TIniFile class (the VCL helper class you use to read / write values to / from INI
Joking aside, I'll implement this code using INI files, but you could easily change this to use the registry.
Here's what we need to do. When the form is created, we need to retrieve the form's size and position from the INI file, and set the corresponding properties. When the form is destroyed, we need to save those values back to the INI file. Of course, you would implement this in some base form so that all forms which inherit from this form implement this automatically.
Now, you'll want to implement this for multiple forms, and you'll want to use one INI file for the entire application, rather than one for each form. The INI file, then, needs to store the information for multiple forms. We'll implement this by having the INI file contain a section for each form. The section name will be the name of the form's class, which is guaranteed to be unique. Here's a sample INI file storing the location and size information for a form whose class name is TForm1
Listing 1, shown below, shows the form's onCreate event. It assumes the INI file, named Forms.ini, is located in the same directory as the application. The code first instantiates the TIniFile class, saving the object reference in a variable called iniFile (I added this as a new instance variable in this form's public section). It then proceeds to read an entry called top, from the section with the same name as the form's class (Self.ClassName returns the form's className). If it does not find the entry, the code simply exits as there are no values to restore. If it successfully reads the entry named top, it proceeds to read the other entries from the INI file, setting the corresponding form properties.
Listing 2, also shown below, shows the form's onDestroy event. It simply writes the appropriate form properties to the INI file then frees the INI file object.
INI_FILE_NAME = 'Forms.ini';
procedure TForm1.FormCreate(Sender: TObject);
iniFileName : String;
nTop : Integer;
iniFileName := ExtractFilePath( Application.ExeName ) + INI_FILE_NAME;
iniFile := TIniFile.Create( iniFileName );
nTop := iniFile.ReadInteger( self.ClassName, 'Top', -1);
If nTop = -1 Then
self.Top := nTop;
self.left := iniFile.ReadInteger( self.ClassName, 'Left', -1);
self.height := iniFile.ReadInteger( self.ClassName, 'Height', -1);
self.width := iniFile.ReadInteger( self.ClassName, 'Width', -1);
Listing 1 - onCreate event loading values from the INI file
procedure TForm1.FormDestroy(Sender: TObject);
iniFile.WriteInteger(Self.ClassName, 'Top', self.top);
iniFile.WriteInteger(Self.ClassName, 'Left', self.left);
iniFile.WriteInteger(Self.ClassName, 'Height', self.height);
iniFile.WriteInteger(Self.ClassName, 'Width', self.width);
Listing 2 - onDestroy event saving values to the INI file