Free Pascal Center Form

Let’s make a program that centers a couple of forms. One will center relative to the screen, and the other form will be centered relative to the first form. Open Lazarus/Typhon developing environment and create an application like we did on previous examples. Save the project calling this unit as centermain. Add a MainMenu and a XMLPropStorage visual components to this Form.

Right-click on “MainMenu1” and select “Menu Editor …”.  Add  an Item “File”, and four sub-items: “Show”, “Center Main”, “Create Centered” and “Exit”.

Click on File | Show and the source editor will show up. Add the following code to the generated eventhandler:

procedure TForm1.MenuItem2Click(Sender: TObject);
var
R: TRect;
begin
   R := Application.MainForm.BoundsRect;
   //Center Form 2 on Form 1 (Main Form)
   with Form2 do
   { Calculate size/position of Form2, then Show it }
   begin
      Top:=((( R.Bottom-R.Top)-Height)div 2)+ R.Top;
      Left:=(((R.Right-R.Left)-Width)div 2)+ R.Left;
      Show; //It will center second form on the first each time it is executed
   end;
end;

Your event handler may have a different name than MenuItem2Click. It depends on whether you created this menu item before or after the other menu items, or if you dared to change the item’s name. This event handler shows a second form we had not created yet. It aligns the second form to this first form in order to show up in front of this. For menu item Center Main add the following:

procedure TForm1.MenuItem3Click(Sender: TObject);
begin
   //Center Main Form on screen
   Top:=(( Screen.Height-Height)div 2);
   Left:=((Screen.Width-Width)div 2);
   Show; //It will center this form on the screen each time it is executed
end;

This will center the present form on the screen. For menu item Create Centered add the following:

procedure TForm1.MenuItem4Click(Sender: TObject);
var
  f : TForm;
begin
   f := CreateMessageDialog('Test', mtCustom, [mbOK]);
   f.Position := poOwnerFormCenter;
   f.ShowModal;
end;

This will create a Dialog Box waiting for you to pres OK in modal form. You won’t be able to call other forms from this application until you click OK. For menu item Exit add the following:

procedure TForm1.MenuItem5Click(Sender: TObject);
begin
  Close;
end;

Since Form1 is the main form, exiting from it will cause the application to exit. Select XMLPropStorage1. On the properties tab at the Object Inspector we have to set Active as true (check), and FileName as the name of the file were we are going to save the form’s state. As our example is form1pos.xml.

Select Form1 in the Object Inspector, click on sessionProperties, click on the […] button. A form will show up. Then select each of the Form1 properties Left, Top, Width, Height and add them with button [Add]. End with [Ok].

Now we need to create Form2. We add a button [Button1] and save the form’s unit as centform. By double clicking on Button1 we can write its event handler and simply add Close;

procedure TForm2.Button1Click(Sender: TObject);
begin
     Close;
end;

This will close Form2.

Now we can compile and run our program. Just play around and see what happens. Check whether the form stores its position from a previous run.

 

Related posts