gpt4 book ai didi

delphi - Delphi 程序终止时向用户显示消息

转载 作者:行者123 更新时间:2023-12-03 15:42:55 24 4
gpt4 key购买 nike

在某些情况下,我应该终止应用程序

Application.Terminate;

在这种情况下,我想在某些TFrame析构函数内向用户显示一些消息。

我尝试使用 MessageBoxMessageBoxIndirectShowMessage 函数,但没有成功。消息框未出现在屏幕上并且应用程序关闭。

有没有办法在应用程序终止时向用户显示一些消息?

顺便说一句,使用了Delphi XE

最佳答案

点赞评论表示显示消息,例如MessageBoxMessageBoxIndirectShowMessage,您的进程仍需要运行。

Delphi for .NET 会有一个合适的 OnShutdown 事件,但是当不使用条件 CLR 进行编译时,该事件将不存在。

但是,我们可以使用退出过程,就像 TApplication 本身使用 DoneApplication 那样。在调用 System.Halt 之前,在进程仍然存在的时刻调用此过程。它是通过调用System.SysUtils 中的AddExitProc(Proc: TProcedure) 添加的。代码注释如下:

{ AddExitProc adds the given procedure to the run-time library's exit procedure list. When an application terminates, its exit procedures are executed in reverse order of definition, i.e. the last procedure passed to AddExitProc is the first one to get executed upon termination. }

我个人会决定使用这个,尽管 warning from the documentation ,因为 TApplication 本身仍在东京使用它来调用 DoneApplication。文档摘录:

[...]AddExitProc is not compatible with ULX package support and is provided for backward compatibility only. Do not use AddExitProc in new applications.[...]

VCL 项目的小代码示例将在应用程序终止时显示一条消息:

program Project1;

uses
Vcl.Forms,
Vcl.Dialogs,
System.SysUtils,
Unit2 in 'Unit2.pas' {Form2};

{$R *.res}

procedure AppTerminated;
begin
MessageDlg('Message', mtInformation, [mbOk], 0);
end;

begin
AddExitProc(AppTerminated);
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm2, Form2);
Application.Run;
end.

关于delphi - Delphi 程序终止时向用户显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48900854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com