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

标签 delphi delphi-xe6

如果用户在我的主表单上单击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时,(而不是隐藏)表单关闭并且应用程序终止:

enter image description here

...这听起来像是 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;

奖励阅读

最佳答案

当用户关闭窗口时,它会收到一条 WM_CLOSE 消息,该消息会触发 TForm 调用其自身的 Close() 方法。在项目的 MainForm 上调用 Close() 始终 会终止应用程序,因为这是 TCustomForm.Close() 中的硬编码行为:

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() 并返回 caNone:

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

关于delphi - 如何隐藏主窗体而不是关闭它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49303888/

相关文章:

delphi - 如何设置与 TEdit 控件关联的可访问名称?

delphi - 如何恢复原来的IDE Insight窗口?

delphi - 绑定(bind)到 Delphi XE4 编译器的条件编译器指令是什么?

delphi - 停止闪烁

delphi - 代码检测到 MMX/SSE/AVX 但未检测到 AVX2

ios - 控件变得不可见 - 如何修复奇怪的 GUI 行为 - Delphi xe6

delphi - FireDac 查询字段大小不会因 SQL 更改而更新

python - 桌面开发语言是编译二进制还是脚本语言(windows)?

sql - 在已带有'%'符号的字符串上使用格式

database - XE6 Datasnap 向导不可用