且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Delphi中隐藏MDI子窗体?

更新时间:2023-12-06 09:27:10

TCustomForm 中有一个受保护的过程定义为:

  procedure TCustomForm.VisibleChanging; 
begin
if(FormStyle = fsMDIChild)和Visible和(Parent = nil)然后
raise EInvalidOperation.Create(SMDIChildNotVisible);
结束

在您的MDI子窗口中覆盖它:

  procedure TMDIChildForm.VisibleChanging; 
begin
// :-P
end;

这是一个简单的例子



在阅读了Jeroen的评论后,我尝试了另一种解决方案,但是有一点闪烁:

  procedure TMDIChildForm.VisibleChanging; 
begin
if Visible then
FormStyle:= fsNormal
else
FormStyle:= fsMDIChild;
结束

也许这适用于所有Windows版本。



PS:我没有发现Windows 2k3SP2 x86和Windows 7 Ultimate x86上的第一个解决方案有任何问题


How can I hide a MDIChild window in Delphi?

I'm using this code in FormClose() event of my MDI children, but it doesn't seem to work:

procedure TfrmInstrument.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caNone;
  ShowWindow(Handle, SW_HIDE);
  frmMainForm.MDIChildClosed(Handle);
end;

My child window is minimized instead of being hidden.

There is a protected procedure in TCustomForm defined as:

procedure TCustomForm.VisibleChanging;
begin
  if (FormStyle = fsMDIChild) and Visible and (Parent = nil) then
    raise EInvalidOperation.Create(SMDIChildNotVisible);
end;

Override it in your MDI child windows as:

procedure TMDIChildForm.VisibleChanging;
begin
  // :-P
end;

Here's a simple example

After reading Jeroen's comment, I tried another solution which works as well, but with a little flickering:

procedure TMDIChildForm.VisibleChanging;
begin
  if Visible then
    FormStyle := fsNormal
  else
    FormStyle := fsMDIChild;
end;

Maybe this works on all Windows versions.

PS: I didn't find any problem with the first solution on Windows 2k3SP2 x86 and Windows 7 Ultimate x86