WM_CLOSE:关闭应用程序窗口
procedure TCustomForm.WMClose(var Message: TWMClose); begin Close; end; 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; end; end; procedure TCustomForm.WMDestroy(var Message: TWMDestroy); begin if NewStyleControls then SendMessage(Handle, WM_SETICON, 1, 0); if (FMenu <> nil) and (FormStyle <> fsMDIChild) then begin Windows.SetMenu(Handle, 0); FMenu.WindowHandle := 0; end; inherited; end;
还有:
function TApplication.ProcessMessage(var Msg: TMsg): Boolean; var Handled: Boolean; begin Result := False; if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then begin Result := True; if Msg.Message <> WM_QUIT then begin Handled := False; if Assigned(FOnMessage) then FOnMessage(Msg, Handled); if not IsHintMsg(Msg) and not Handled and not IsMDIMsg(Msg) and not IsKeyMsg(Msg) and not IsDlgMsg(Msg) then begin TranslateMessage(Msg); DispatchMessage(Msg); end; end else FTerminate := True; end; end;
DestroyWindow函数来自这里(TApplication.Destroy里也调用了这个函数):
procedure TWinControl.DestroyWindowHandle; begin Include(FControlState, csDestroyingHandle); try if not Windows.DestroyWindow(FHandle) then RaiseLastOSError; finally Exclude(FControlState, csDestroyingHandle); end; FHandle := 0; end;
那难道每个TButton,每个TPanel,都会收到WM_DESTROY消息吗?