7

伙计们,我想知道是否有人知道在所有 MDI 表单关闭时我可以拦截的任何事件或方法。

例子:

我想在我的主窗体中实现一个事件,当我关闭所有 MDI 窗体时,会触发这样的事件。

如果有人可以提供帮助,将不胜感激。

4

3 回答 3

8

MDI 子窗体(实际上是任何窗体)在被销毁时会通知主窗体。您可以使用此通知机制。例子:

type
  TForm1 = class(TForm)
    ..
  protected
    procedure Notification(AComponent: TComponent; Operation: TOperation);
      override;

  ..

procedure TForm1.Notification(AComponent: TComponent; Operation: TOperation);
begin
  inherited;
  if (Operation = opRemove) and (AComponent is TForm) and
      (TForm(AComponent).FormStyle = fsMDIChild) and
      (MDIChildCount = 0) then begin

    // do work

  end;
end;
于 2013-09-03T21:24:40.410 回答
4

捕获WM_MDIDESTROY发送到 MDI 客户端窗口的消息:

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FOldClientWndProc: TFarProc;
    procedure NewClientWndProc(var Message: TMessage);
  end;

...

procedure TForm1.FormCreate(Sender: TObject);
begin
  if FormStyle = fsMDIForm then
  begin
    HandleNeeded;
    FOldClientWndProc := Pointer(GetWindowLong(ClientHandle, GWL_WNDPROC));
    SetWindowLong(ClientHandle, GWL_WNDPROC,
      Integer(MakeObjectInstance(NewClientWndProc)));
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  SetWindowLong(ClientHandle, GWL_WNDPROC, Integer(FOldClientWndProc));
end;

procedure TForm1.NewClientWndProc(var Message: TMessage);
begin
  if Message.Msg = WM_MDIDESTROY then
    if MDIChildCount = 1 then
      // do work
  with Message do
    Result := CallWindowProc(FOldClientWndProc, ClientHandle, Msg, WParam,
      LParam);
end;
于 2013-09-03T21:36:15.333 回答
2

您可以让 MainForm 为其创建的每个 MDI 子项分配一个OnClose或事件处理程序。OnDestroy每次关闭/销毁 MDI 客户端时,处理程序都可以检查是否还有更多 MDI 子窗体仍然打开,如果没有,则执行它需要执行的任何操作。

procedure TMainForm.ChildClosed(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;

  // the child being closed is still in the MDIChild list as it has not been freed yet...
  if MDIChildCount = 1 then
  begin
    // do work
  end;
end;

或者:

const
  APPWM_CHECK_MDI_CHILDREN = WM_APP + 1;

procedure TMainForm.ChildDestroyed(Sender: TObject);
begin
  PostMessage(Handle, APPWM_CHECK_MDI_CHILDREN, 0, 0);
end;

procedure TMainForm.WndProc(var Message: TMessage);
begin
  if Message.Msg = APPWM_CHECK_MDI_CHILDREN then
  begin
    if MDIChildCount = 0 then
    begin
      // do work
    end;
    Exit;
  end;
  inherited;
end;
于 2013-09-04T00:14:35.167 回答