Question

How do I control the placement of an MDI child window (FormStyle := fsMDIChild) in Delphi or C++Builder? I know that I can set Left, Top, Position, and so on, but for an MDI child in particular, these don't take effect until after the window has already been created and shown in its default location. The result is that creating and positioning several windows at once results in a fair bit of flicker as windows are created in their default positions then immediately moved and resized.

From delving into the VCL source, the only solution I've been able to find is to override TCustomForm's CreateParams method and change the X, Y, Width, and Height fields of the Params parameter, but that feels like a hack. Is there a cleaner way of doing this?

Was it helpful?

Solution

I observe no flickering at all, but that might be because my computer is too fast or it might be an improvement in Windows 7 to reduce flickering.

I set the MDI child window position at its FormShow:

procedure TForm2.FormShow(Sender: TObject);
begin
  Top := 200;
  Left := 400;
end;

OTHER TIPS

You can send WM_SETREDRAW messages to the MainForm's ClientHandle, one with the wParam set to False, and then later with the wParam set to True, to avoid flickering while you are setting up an MDI child window, eg:

Delphi:

SendMessage(Application.MainForm.ClientHandle, WM_SETREDRAW, False, 0);
try
  Child := TChildForm.Create(Self);
  Child.Left := ...;
  Child.Top := ...;
  Child.Show;
finally
  SendMessage(Application.MainForm.ClientHandle, WM_SETREDRAW, True, 0);
  InvalidateRect(Application.MainForm.ClientHandle, nil, True);
end;

C++:

SendMessage(Application->MainForm->ClientHandle, WM_SETREDRAW, FALSE, 0);
try
{
  Child = new TChildForm(this);
  Child->Left = ...;
  Child->Top = ...;
  Child->Show();
}
__finally
{
  SendMessage(Application->MainForm->ClientHandle, WM_SETREDRAW, TRUE, 0);
  InvalidateRect(Application->MainForm->ClientHandle, NULL, TRUE);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top