Mirror

How to avoid flicker when moving or sizing a MDI child form (Views: 711)


Problem/Question/Abstract:

I have an MDI application with many child forms. Their windowstate property is set to maximized. When a child form is created and shown, the form visibly resizes as it is shown. I would like the child form to open already maximized without the visible resizing. Any ideas?

Answer:

Preventing visible flicker when moving or sizing MDI children directly after their creation:

{ ... }
public
{ Public declarations }

constructor Create(aOwner: TComponent); override;
end;

implementation

{$R *.DFM}

constructor TMDIChild.Create(aOwner: TComponent);
var
  crect: TRect;
  chandle: HWND;
begin
  { Get handle of MDI client window }
  chandle := application.mainform.clienthandle;
  { Block redrawing of this window and its children }
  Lockwindowupdate(chandle);
  { Create this MDI child at default position, it will not be drawn yet
        due to the update block }
  inherited Create(aOwner);
  { Get the client windows rect and center this form in it }
  Windows.GetClientrect(chandle, crect);
  SetBounds((crect.right - width) div 2, (crect.bottom - height) div 2, width,
    height);
  { Release the block, this allows the form to redraw at the new position }
  LockWindowUpdate(0);
end;

<< Back to main page