下面的演示应用程序会在用户尝试关闭表单时询问他们是否真的要关闭。
当表单未最小化时,这可以正常工作。但是,如果他们在表单最小化时尝试从任务栏关闭应用程序,应用程序将被锁定并且只能从任务管理器关闭。
似乎显示了模态警告消息但不可见。由于他们无法关闭模态消息框,所以他们根本无法关闭它,即使在恢复窗口后也是如此。
有人对解决方案有任何想法吗?
我试过在显示消息框之前强制恢复窗体的 WindowState,但它没有改变任何东西。
我正在使用同步消息框,因为这是为了防止丢失我的应用程序中未保存的工作。我不希望用户在消息框中单击“是”之前能够关闭应用程序。
DFM:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 347
ClientWidth = 437
FormFactor.Width = 320
FormFactor.Height = 480
FormFactor.Devices = [Desktop]
OnClose = FormClose
DesignerMasterStyle = 0
end
代码:
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs;
type
TForm1 = class(TForm)
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
uses
FMX.DialogService.Sync;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if TDialogServiceSync.MessageDialog(
'Are you sure you want to close?', TMsgDlgType.mtWarning,
[TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo],
TMsgDlgBtn.mbYes, 0) = mrNo then
Action := TCloseAction.caNone;
end;
end.