如果用户在我的主表单上单击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;
奖励阅读
- How to make hovering over Minimize, Maximize, and Close buttons behave?
- Hide form instead of closing when close button clicked
- How to show a modal dialog from a modeless form? (Windows、WinForms、WPF、MessageBox、TaskDialog、ProgressDialog、SHFileOperation、IFileOperation 都搞错了吗?没有人使用过无模式窗口吗?)
最佳答案
当用户关闭窗口时,它会收到一条 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/