2013-09-03 9 views
5

皆、誰かがすべてのMDIフォームが閉じられたときに私が傍受できるイベントや方法を知っていれば嬉しいです。すべてのmdiフォームが閉じられたときのイベント

例:

私は、すべてのMDIフォームを閉じると、このようなイベントがトリガされた私のメインフォームでイベントを実装します。

誰かが助けることができれば幸いです。

答えて

7

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; 
+0

1ナイス!私よりも良い。 ;-) – NGLN

+0

@NGLN - ありがとう!子供がこれをいつ行うのかを知る必要がある場合に備えて、あなたのものはより強力です。:) –

+1

NGLN、Sertac Akyus、Remy Lebeau。すべての優秀なあなたの答えに感謝します。あなたはとても良い。この状況では、最高のコードはSertac Akyuzでした。もっとシンプルで、私の問題を解決しました。 NGLNとRemy、将来の状況のた​​めにコードを保存しました。ありがとう。 – Delphiman

4

は、MDIクライアントウィンドウに送信WM_MDIDESTROYメッセージをキャッチ:

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

あなたは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; 
関連する問題