所有mdi表格关闭时的事件

时间:2013-09-03 21:02:03

标签: forms delphi events mdi

伙计们,如果有人知道在所有MDI表格关闭时我可以拦截任何事件或方法,我想要。

示例:

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

感激如果有人可以提供帮助。

3 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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;

答案 2 :(得分:2)

您可以让MainForm为其创建的每个MDI子项分配OnCloseOnDestroy事件处理程序。每次关闭/销毁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;