There is a strange problem in the MDI app, when you perform the following code, you will see a strange result.
Suppose you have opened 3 MDI sub-windows.
Procedure Button1Click (Sender: Tobject)
Begin
ShowMessage (INTTOSTR (MDICHILDCOUNT)); // Obvious results equal to 3
If Assignd (ActiveMdichild) Then ActiveMdichild.close;
ShowMessage (INTTOSTR (MDICHILDCOUNT)); / / This time should be a few?
END;
Very strange results are not like thinking because it is 2, but still 3, why?
How to explain this phenomenon? Because it has not yet been released. So what it will be released. procedure TCustomForm.Close; var CloseAction: TCloseAction; begin if fsModal in FFormState then ModalResult: = mrCancel else if CloseQuery then begin if FormStyle = fsMDIChild then if biMinimize in BorderIcons then CloseAction: = caMinimize else CloseAction: = caNone else CloseAction: = caHide; doClose (CloseAction); if CloseAction <> caNone then if Application.MainForm = Self then Application.Terminate else if CloseAction = caHide then Hide else if CloseAction = caMinimize then WindowState: = wsMinimized else Release; // Release see what is? End; End; {Very simple message} Procedure TCUSTOMMM.RELEASE; Begin PostMessage (Handle, CM_RELEASE, 0, 0);
Procedure Cmrelease (Var Message: TMESSAGE); Message CM_RELEASE
Procedure CmtextChanged (Var Message: TMESSAGE); Message CM_TextChange; Procedure CMUIAACTIVATE (MSSAGE CM_UIAACTIVATE)
Procedure tcustom form.cmrelease; begin free; // The real free is here.
END;
From the above code, you can basically guess why there will be the above phenomenon. Activemdichild.close SendMessage (Handle, Cmrelease); // Message Enter Message Queuings do not immediately practice
ShowMessage (INTTOSTR (MDICHINDCOUNT)); // There is no real free. // So I got the wrong result, I know that the reason is better. Next we will solve it.
Procedure Button1Click (Sender: TObject) Var Ret: Boolean; Msg: tagMsg; Begin ShowMessage (IntToStr (MDIChildCount)); // obviously results equal to 3 If Assigned (ActiveMDIChild) then Begin ActiveMDIChild.Close; Ret: = GetMessage (msg, Activemdichild.Handle, 0, 0); if Ret theen if msg.Message = cmrelease damdivemdichild.destroy; // pronounced death in advance, troubleshoot. END;
ShowMessage (INTTOSTR (MDICHILDCOUNT)); // The result is correct. END;
Related resources: c # mdiform multi-view window