Search code examples
delphivclmdichild

Why my MDIChild forms not open in wsMaximized state every time?


Delphi MDIChild windowState problem

I have a delphi project with a MDI and childs. When I created a new child form, this occupies perfctly a reserved space on main form (child wsMaximized).

But when a create another child form (without closed first child), the first child "lost a wsMaximized state".

Resume: I need the child forms maximized every time, but when second child is opened, the "windowstate" another childs is changed.

First child:

procedure TfrmPrincipal.PosicionarForm(AForm: TForm);
var
  Rect: TRect;
begin
  GetWindowRect(Application.MainForm.ClientHandle, Rect);

  InflateRect(Rect, -2 * GetSystemMetrics(SM_CXBORDER),
    -2 * GetSystemMetrics(SM_CYBORDER));
  OffsetRect(Rect, -Rect.Left, -Rect.Top);

  AForm.BoundsRect := Rect;
end;

procedure TfrmPrincipal.actCadastroFornecedorExecute(Sender: TObject);
begin
    frmCadastroFornecedor := TfrmCadastroFornecedor.Create(Application);
    PosicionarForm(frmCadastroFornecedor);
    frmCadastroFornecedor.Show;
  svSub.Visible := False;
  SV.Opened := False;
end;

enter image description here

Secound child:

procedure TfrmPrincipal.actCadastroProdutosExecute(Sender: TObject);
begin
  frmCadastroProduto := TfrmCadastroProduto.Create(Application);
  PosicionarForm(frmCadastroProduto);

  frmCadastroProduto.Show;

  svSub.Visible := False;
  SV.Opened := False;
end;

enter image description here

EDIT:

I created a new project, have 3 forms. The code is very simple, but this behavior continues.

Main FORM:

 unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus;

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    screen11: TMenuItem;
    screen21: TMenuItem;
    procedure screen11Click(Sender: TObject);
    procedure screen21Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
unit2, unit3;

{$R *.dfm}

procedure TForm1.screen11Click(Sender: TObject);
begin
  form2 := tform2.Create(Application);
  form2.Show;
end;

procedure TForm1.screen21Click(Sender: TObject);
begin
form3 := tform3.Create(Application);
  form3.Show;
end;

end.

Child 1:

unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
  TForm2 = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

end.

Child 2:

unit Unit3;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
  TForm3 = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

procedure TForm3.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

end.

Solution

  • After some trial and error (once again an example of how important a complete, minimal, reproducible, example is) I was able to reproduce the error you ask about. If you had included the the .dfm files, the actual problem had been clear much sooner.

    Now, to the problem that the WindowState of the forms change from wsMaximized to wsNormal after deletion of one of the childforms.

    This happens when you have removed biMaximise from BorderIcons for forms Form2 and Form3

    Cure: Let BorderIcons.biMaximise be selected.

    I also suggest you remove PosicionarForm(AForm: TForm); from the main form. It doesn't appear to do anything useful.