Search code examples
delphibordermdimaximize

Delphi: Maximized Child Form in MDI Application


How can I maximize a child window that fits only the client area but not an entire parent window? I don't want that the child window disappears under a main menu or other controls of the parent window.

I have this code

procedure WMSIZE(var Msg: TMessage); message WM_SIZE;

procedure TForm2.WMSIZE(var Msg: TMessage);
begin
  inherited;
  if Msg.WParam = SIZE_MAXIMIZED then
  begin
    ShowWindow(Handle, SW_RESTORE);
    Left := 0;
    Top := 0;
    Width := Form1.ClientWidth - 4; // The BORDER
    Height := Form1.ClientHeight - 4;
  end;
end;

But it's not good enough. The window is actually not maximized. If to change SW_RESTORE to SW_MAXIMIZE then the child window looks buggy.


Solution

  • Normally, the MDI main form's client space should be calculated automatically to the space without menu or bars, provided that those bars are aligned to an edge of the form.

    When a bar or other controls are not aligned, then you indeed have to adjust yourself. Handle WM_NCCALCSIZE to tell windows your form has deviating client rect dimensions.

    Or take a look at NLDExtraMDIProps in which I catch WM_SYSCOMMAND when WParam and $FFF0 = SC_MAXIMIZE to adjust the size of a MDI child window. The component provides in a few extra properties like: BackgroundPicture, CleverMaximizing, ShowClientEdge and ShowScrollBars.