如何隐藏主窗体而不是关闭它?

How to hide main form rather than closing it?

如果用户在我的主窗体上单击 X,我希望该窗体隐藏而不是关闭。这听起来像是 OnClose form event:

的工作

Use OnClose to perform special processing when the form closes. The OnClose event specifies which event handler to call when a form is about to close. The handler specified by OnClose might, for example, test to make sure all fields in a data-entry form have valid contents before allowing the form to close.

A form is closed by the Close method or when the user chooses Close from the form's system menu.

The TCloseEvent type points to a method that handles the closing of a form. The value of the Action parameter determines if the form actually closes. These are the possible values of Action:

  • caNone: The form is not allowed to close, so nothing happens.
  • caHide: The form is not closed, but just hidden. Your application can still access a hidden form.
  • caFree: The form is closed and all allocated memory for the form is freed.
  • caMinimize: The form is minimized, rather than closed. This is the default action for MDI child forms.

我用一种形式在空应用程序中测试:

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
    Action := caHide;
end;

所以现在当我单击 X 时,(而不是隐藏)表单关闭并且应用程序终止:

...这听起来像是 OnClose 事件的工作...

红利阅读

Vcl.Forms.pas

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
      begin
         if Application.MainForm = Self then //Borland doesn't hate developers; it just hates me
            Application.Terminate
         else if CloseAction = caHide then   
            Hide
         else if CloseAction = caMinimize then 
            WindowState := wsMinimized
         else 
            Release;
      end;
   end;
end;

红利阅读

尝试 OnCloseQuery 活动。隐藏表单并将 CanClose 设置为 False。你应该好好的。

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  Hide;
  CanClose := False;
end;

当用户关闭 window 时,它会收到一条 WM_CLOSE 消息,这会触发 TForm 对其自身调用其 Close() 方法。在项目的 MainForm 上调用 Close() 总是 终止应用程序,因为这是 TCustomForm.Close() 中的 hard-coded 行为:

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 // <-- HERE
        else if CloseAction = caHide then Hide
        else if CloseAction = caMinimize then WindowState := wsMinimized
        else Release;
    end;
end;

只有次要 TForm 对象尊重 OnClose 处理程序的输出。

要完成您要求的操作,您可以:

  • 直接处理WM_CLOSE,跳过Close().

    private
      procedure WMClose(var Message: TMessage); message WM_CLOSE;
    
    procedure TForm1.WMClose(var Message: TMessage);
    begin
      Hide;
      // DO NOT call inherited ...
    end;
    
  • 让 MainForm 的 OnClose 处理程序直接调用 Hide() 和 return caNone:

    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      Hide;
      Action := caNone;
    end;