delphi 关闭表单_如何在Delphi中创建,使用和关闭表单

delphi 关闭表单

In Delphi, every project has at least one window -- the program's main window. All windows of a Delphi application are based on TForm object.

Delphi中 ,每个项目都有至少一个窗口-程序的主窗口。 Delphi应用程序的所有窗口均基于TForm对象

形成 ( Form )

Form objects are the basic building blocks of a Delphi application, the actual windows with which a user interacts when they run the application. Forms have their own properties, events, and methods with which you can control their appearance and behavior. A form is actually a Delphi component, but unlike other components, a form doesn't appear on the component palette.

表单对象是Delphi应用程序的基本构建块,是用户运行应用程序时与之交互的实际窗口。 表单具有它们自己的属性,事件和方法,可以用来控制它们的外观和行为。 表单实际上是Delphi组件,但是与其他组件不同,表单不会出现在组件面板上。

We normally create a form object by starting a new application (File | New Application). This newly created form will be, by default, the application's main form - the first form created at runtime.

通常,我们通过启动新应用程序(“文件” |“新应用程序”)来创建表单对象。 默认情况下,此新创建的表单将是应用程序的主表单-运行时创建的第一个表单。

Note: To add an additional form to Delphi project, select File|New Form.

注意:要将其他表单添加到Delphi项目中,请选择“文件” |“新表单”。

出生时间 ( Birth )

OnCreateThe OnCreate event is fired when a TForm is first created, that is, only once. The statement responsible for creating the form is in the project's source (if the form is set to be automatically created by the project). When a form is being created and its Visible property is True, the following events occur in the order listed: OnCreate, OnShow, OnActivate, OnPaint.

OnCreate第一次创建TForm时(即仅一次),将触发OnCreate事件。 负责创建表单的语句位于项目的源代码中(如果表单设置为由项目自动创建)。 当创建窗体并且其Visible属性为True时,将按照列出的顺序发生以下事件:OnCreate,OnShow,OnActivate,OnPaint。

You should use the OnCreate event handler to do, for example, initialization chores like allocating string lists.

例如,您应该使用OnCreate事件处理程序来执行初始化工作,例如分配字符串列表。

Any objects created in the OnCreate event should be freed by the OnDestroy event.

OnDestroy事件应释放在OnCreate事件中创建的任何对象。

 OnCreate -> OnShow -> OnActivate -> OnPaint -> OnResize -> OnPaint ... 

OnShowThis event indicates that the form is being displayed. OnShow is called just before a form becomes visible. Besides main forms, this event happens when we set forms Visible property to True, or call the Show or ShowModal method.

OnShow此事件表示正在显示表单。 在表单可见之前调用OnShow。 除了主窗体外,当我们将窗体的Visible属性设置为True或调用Show或ShowModal方法时,也会发生此事件。

OnActivateThis event is called when the program activates the form - that is, when the form receives the input focus. Use this event to change which control actually gets focus if it is not the one desired.

OnActivate当程序激活窗体时,即窗体收到输入焦点时,将调用此事件。 使用此事件可以更改哪个控件真正获得焦点(如果不是所需的控件)。

OnPaint, OnResizeEvents like OnPaint and OnResize are always called after the form is initially created, but are also called repeatedly. OnPaint occurs before any controls on the form are painted (use it for special painting on the form).

OnPaint,OnResize事件(如OnPaint和OnResize)总是在最初创建表单后被调用,但也被重复调用。 OnPaint在绘制窗体上的任何控件之前发生(将其用于窗体上的特殊绘制)。

生活 ( Life )

The birth of a form is not so interesting as its life and death can be. When your form is created and all the controls are waiting for events to handle, the program is running until someone tries to close the form!

形式的诞生并不像它的生死那样有趣。 当您创建表单并且所有控件都在等待事件处理时,该程序将运行,直到有人试图关闭该表单!

死亡 ( Death )

An event-driven application stops running when all its forms are closed and no code is executing. If a hidden form still exists when the last visible form is closed, your application will appear to have ended (because no forms are visible), but will in fact continue to run until all the hidden forms are closed. Just think of a situation where the main form gets hidden early and all other forms are closed.

当一个事件驱动的应用程序的所有表单都关闭且没有代码执行时,它将停止运行。 如果在关闭最后一个可见表单时仍存在隐藏表单,则您的应用程序似乎已结束(因为没有可见的表单),但实际上它将继续运行,直到所有隐藏表单都被关闭。 试想一下这样一种情况,即主要表单会被早期隐藏,而所有其他表单都将被关闭。

 ... OnCloseQuery -> OnClose -> OnDeactivate -> OnHide -> OnDestroy 

OnCloseQueryWhen we try to close the form using the Close method or by other means (Alt+F4), the OnCloseQuery event is called. Thus, event handler for this event is the place to intercept a form's closing and prevent it. We use the OnCloseQuery to ask the users if they are sure that they realy want the form to close.

OnCloseQuery当我们尝试使用Close方法或其他方式(Alt + F4)关闭窗体时,将调用OnCloseQuery事件。 因此,此事件的事件处理程序是拦截表单关闭并阻止表单关闭的地方。 我们使用OnCloseQuery来询问用户是否确实希望关闭表单。

 procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean) ;
begin
   if MessageDlg('Really close this window?', mtConfirmation, [mbOk, mbCancel], 0) = mrCancel then CanClose := False;
end; 

An OnCloseQuery event handler contains a CanClose variable that determines whether a form is allowed to close. The OnCloseQuery event handler may set the value of CloseQuery to False (via the CanClose parameter), aborting the Close method.

OnCloseQuery事件处理程序包含一个CanClose变量,该变量确定是否允许关闭窗体。 OnCloseQuery事件处理程序可以将CloseQuery的值设置为False(通过CanClose参数),从而中止Close方法。

OnCloseIf OnCloseQuery indicates that the form should be closed, the OnClose event is called.

OnClose如果OnCloseQuery指示应关闭该窗体,则调用OnClose事件。

The OnClose event gives us one last chance to prevent the form from closing. The OnClose event handler has an Action parameter, with the following four possible values:

OnClose事件为我们提供了防止表单关闭的最后机会。 OnClose事件处理程序具有一个Action参数,具有以下四个可能的值:

  • caNone. The form is not allowed to close. Just as if we have set the CanClose to False in the OnCloseQuery.

    不能 。 该表格不允许关闭。 就像我们在OnCloseQuery中将CanClose设置为False一样。

  • caHide. Instead of closing the form you hide it.

    H。 无需关闭表单,而是将其隐藏。

  • caFree. The form is closed, so it's allocated memory is freed by Delphi.

    caFree 。 该窗体已关闭,因此已分配的内存由Delphi释放。

  • caMinimize. The form is minimized, rather than closed. This is the default action for MDI child forms. When a user shuts down Windows, the OnCloseQuery event is activated, not the OnClose. If you want to prevent Windows from shutting down, put your code in the OnCloseQuery event handler, of course CanClose=False will not do this.

    caMinimize 。 该窗体被最小化,而不是关闭。 这是MDI子窗体的默认操作。 当用户关闭Windows时,将激活OnCloseQuery事件,而不是OnClose。 如果要防止Windows关闭,请将代码放在OnCloseQuery事件处理程序中,当然CanClose = False不会执行此操作。

OnDestroyAfter the OnClose method has been processed and the form is to be closed, the OnDestroy event is called. Use this event for operations opposite to those in the OnCreate event. OnDestroy is used to deallocate objects related to the form and free the corresponding memory.

OnDestroy在处理OnClose方法并关闭窗体后,将调用OnDestroy事件。 将此事件用于与OnCreate事件相反的操作。 OnDestroy用于取消分配与表单有关的对象并释放相应的内存。

When the main form for a project closes, the application terminates.

当项目的主窗体关闭时,应用程序终止。

翻译自: https://www.thoughtco.com/life-cycle-of-a-delphi-form-1058011

delphi 关闭表单

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值