Avoid New MDI Child Resizing Animation (and Delay) in Delphi MDI Applications

Avoid New MDI Child Resizing Animation (and Delay) in Delphi MDI Applications


Lock (Prevent) Window Updating When Creating New MDI Child Forms
By Zarko Gajic

 

If you are creating MDI applications using Delphi, you must have noticed some "quirks" or issues that you cannot simply handle / fix from your code.

 

MDI interface was designed in the days of Windows 3 (some 10+ years ago) and it was designed with a single type of application in mind: a parent window that hosts multiple instances of the same class of "document" window (just think of you first MS Word).

 

Nasty MDI Child Resizing Animation


When creating (to show) an MDI child an animation of resizing will take place. This animation might look ugly if the code executed during the creation of your MDI child takes some time to process.


Even if WindowState property was set to wsMaximized when an MDI child is created it will be created using Default Pos at X, Y coordinates where you left your form at design time.

Windows simply insists on creating MDI children visible and at a default position.

After creation, the MDI child will get maximized but an ugly animation will take place.

 

Quickly Create MDI Children - Eliminate "Create & Resize" Animation


To eliminate the MDI child creation and resizing animation you can send a special message to the MDI parent form, WM_SETREDRAW. The WM_SETREDRAW can be sent to a window to enable changes in that window to be redrawn or to prevent changes in that window from being redrawn.
To prevent the animation flicker, have the next code in your MDI Parent form's unit:

 

TMDIMainForm = class(TForm)
private
  fLockClientWindowUpdateCount: Integer;
public
  constructor Create(aOwner: TComponent) ; override;
  procedure LockClientWindowUpdate;
  procedure UnlockClientWindowUpdate;
end;

...

 

constructor TMDIMainForm.Create(aOwner: TComponent) ;
begin
  inherited Create(aOwner) ;
  fLockClientWindowUpdateCount := 0;
end;

 

procedure TMDIMainForm.LockClientWindowUpdate;
begin
  if fLockClientWindowUpdateCount = 0 then SendMessage(ClientHandle, WM_SETREDRAW, 0, 0) ;
  Inc(fLockClientWindowUpdateCount) ;
end;

 

procedure TMDIMainForm.UnlockClientWindowUpdate;
begin
  Dec(fLockClientWindowUpdateCount) ;
  if fLockClientWindowUpdateCount = 0 then
  begin
    SendMessage(ClientHandle, WM_SETREDRAW, 1, 0) ;
    RedrawWindow(ClientHandle, nil, 0, RDW_FRAME or RDW_INVALIDATE or RDW_ALLCHILDREN or RDW_NOINTERNALPAINT)
  end
end;

 

Now, when you need to create (and show) an MDI client form(s), just call LockClientWindowUpdate and UnlockClientWindowUpdate.
If a client window takes some time to create, you can change the cursor to let the user something (form creation) is going on:

 

LockClientWindowUpdate;
Screen.Cursor := crHourGlass;
try
  Application.CreateForm(MDIChildForm) ;
finally
  Screen.Cursor := crDefault;
  UnlockClientWindowUpdate;
end;

 

That's it. Now your MDI child forms will load faster and without the confusing animation.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值