2013-09-03 8 views
5

Ragazzi, mi piacerebbe se qualcuno conoscesse qualsiasi evento o metodo che posso intercettare quando tutti i moduli MDI sono stati chiusi.Evento quando tutti i moduli mdi sono chiusi

Esempio:

Voglio realizzare un evento nella mia forma principale dove quando chiudo tutte le forme MDI, un tale evento è stato attivato.

Grato se qualcuno può aiutare.

risposta

7

I moduli figlio MDI (in pratica qualsiasi forma), mentre vengono distrutti, invieranno una notifica al modulo principale. È possibile utilizzare questo meccanismo di notifica. Esempio:

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 Nizza! Meglio del mio. ;-) – NGLN

+0

@NGLN - Grazie! La tua è più potente, nel caso in cui tu abbia bisogno di sapere quando un bambino fa questo e quello ... :) –

+1

NGLN, Sertac Akyus e Remy Lebeau. Grazie per le tue risposte, tutte eccellenti. Sei molto bravo. Per questa mia situazione, il codice migliore era Sertac Akyuz. È più semplice e risolto il mio problema. NGLN e Remy, ho salvato il tuo codice per le situazioni future. Grazie. – Delphiman

4

Cattura il messaggio WM_MDIDESTROY inviare alla finestra MDI di client:

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

Si può avere il MainForm assegnare un gestore di eventi OnClose o OnDestroy ad ogni figlio MDI che crea. Ogni volta che un client MDI viene chiuso/distrutto, il gestore può controllare se altri moduli figlio MDI sono ancora aperti e, in caso contrario, fare ciò che è necessario fare.

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; 

Oppure:

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;