如果用户在我的主窗体上单击X,我希望窗体隐藏而不是关闭。这听起来像是使用OnClose表单事件的工作:
使用OnClose在窗体关闭时执行特殊处理。OnClose事件指定在窗体即将关闭时调用哪个事件处理程序。由OnClose指定的处理程序可能会检查数据输入窗体中所有字段是否具有有效内容,然后才允许窗体关闭。
通过Close方法或当用户选择关闭窗口的系统菜单时,窗体被关闭。
TCloseEvent类型指向处理窗体关闭的方法。Action参数的值确定窗体是否实际关闭。以下是Action的可能值:
- caNone:不允许关闭窗体,因此什么也不会发生。 - caHide:窗体未关闭,而是隐藏了起来。您的应用程序仍然可以访问一个隐藏的窗体。 - caFree:窗体已关闭,窗体分配的所有内存都已释放。 - caMinimize:窗体被最小化,而不是关闭。这是MDI子窗体的默认操作。
我在一个空应用程序中测试了一下。
现在,当我点击X时,表单不再隐藏,而是关闭应用程序:
使用OnClose在窗体关闭时执行特殊处理。OnClose事件指定在窗体即将关闭时调用哪个事件处理程序。由OnClose指定的处理程序可能会检查数据输入窗体中所有字段是否具有有效内容,然后才允许窗体关闭。
通过Close方法或当用户选择关闭窗口的系统菜单时,窗体被关闭。
TCloseEvent类型指向处理窗体关闭的方法。Action参数的值确定窗体是否实际关闭。以下是Action的可能值:
- caNone:不允许关闭窗体,因此什么也不会发生。 - caHide:窗体未关闭,而是隐藏了起来。您的应用程序仍然可以访问一个隐藏的窗体。 - caFree:窗体已关闭,窗体分配的所有内存都已释放。 - caMinimize:窗体被最小化,而不是关闭。这是MDI子窗体的默认操作。
我在一个空应用程序中测试了一下。
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;
额外阅读
- 如何使最小化、最大化和关闭按钮的悬停行为更加友好?
- 当点击关闭按钮时,如何隐藏窗体而不是关闭它?
- 如何从无模式窗体中显示模态对话框? (Windows、WinForms、WPF、MessageBox、TaskDialog、ProgressDialog、SHFileOperation、IFileOperation 都做错了吗?没有人使用无模式窗口吗?)