How to place a form before showing it?

Our application uses a common base form from which all forms should have been inherited. I would like to get rid of it for a number of reasons, ranging from the need for the police, so that everyone uses it for several troubles associated with the implementation of Delphi VFI. It turns out that the bulk of the proposed functions can be performed in other, more reliable ways.

The one I'm not very sure about automatically positions all forms in the center of its subscribers. Therefore, if I open Dialog A from my main form, it should be placed above the center of the main form. And if I then open Dialog B from dialog A, it should be placed above the center of dialog A, etc.

We took care of all this by setting the base property of Position position in poOwnerFormCenter, and it did a great job. But how to do this for the entire application?

I thought about using Screen.OnActiveFormChange, but I think this happens every time the form gets focus. I also thought about using Application.OnModalBegin, but there seems to be no obvious way to find the form in the place it is called.

Has anyone tried this?

+5
source share
5 answers

Well, obviously, the form of inheritance is provided to solve exactly the problem that you are trying to solve. Any decision is likely to lead to an incorrect imitation of form inheritance.

- , "= class (TForm)" TForm , , , ?

TForm, . , .

+5

, . , , . , , role, , , , .

, , . , .

+4

, , - , , , .

0

FormShow , SetBounds(). , CMShowing.

0

OnModalBegin . - "Hack", , , . .

procedure TMainForm.Button1Click(Sender: TObject);
var
  mForm: TForm;
begin
  mForm := TForm.create(self);
  mform.width := 300;
  mform.height := 300;
  mForm.ShowModal;
  mForm.Free;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  application.OnModalBegin := modalbegin;
end;

procedure TMainForm.FormShow(Sender: TObject);
begin
  if Screen.FormCount>1 then begin
    screen.forms[Screen.FormCount-1].left := round((screen.forms[Screen.FormCount-2].left + screen.forms[Screen.FormCount-2].width/2) - screen.forms[Screen.FormCount-1].width/2);
    screen.forms[Screen.FormCount-1].top := round((screen.forms[Screen.FormCount-2].top + screen.forms[Screen.FormCount-2].height/2) - screen.forms[Screen.FormCount-1].height/2);
    application.processmessages;
    screen.forms[Screen.FormCount-1].Caption  := inttostr(screen.forms[Screen.FormCount-1].top)+','+inttostr(screen.forms[Screen.FormCount-1].left);
  end;
end;

procedure TMainForm.ModalBegin(Sender: TObject);
begin
  if Screen.FormCount>=0 then
    screen.forms[Screen.FormCount-1].OnShow := FormShow;
end;
0

All Articles