如何处理close事件并使应用程序在Delphi中运行

时间:2014-11-11 19:38:40

标签: delphi firemonkey delphi-xe7

在CloseQueryEvent中,我已经为Application Close确认添加了MessageDlg代码,当我在Windows中运行应用程序时,如果我正在传递CanClose = True,则应用程序正常运行。但是这个消息对话在Android中不起作用,请建议我有其他方法来处理closeQuery事件。我也见过How to close android app in Delphi-XE5 Firemonkey application?

的例子

3 个答案:

答案 0 :(得分:2)

模态对话框在移动平台上不起作用。您必须使用以匿名过程作为输入的异步版本,然后在关闭对话框时在该过程中执行所需的操作。例如:

procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := False;
  MessageDlg('Do you really want to exit?', TMsgDlgType.mtConfirmation, [TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo], 0,
    procedure(const AResult: TModalResult)
    begin
      if AResult = mrYes then
        Application.Terminate;
    end
  );
end;

答案 1 :(得分:1)

procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;  var KeyChar: Char; Shift: TShiftState);
begin
  if Key = vkHardwareBack then
  begin
    MessageDlg('Do You Want To Close This Application', TMsgDlgType.mtConfirmation,
    [TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo], 0,
    procedure(const AResult: TModalResult)
    begin
      if AResult = mrYes then
      begin
        Application.Terminate;
        Exit;
      end
    end);
    Key := 0;
  end;
end;

这段代码对我来说很好

答案 2 :(得分:0)

根据雷米的回答。

模态对话框在移动平台上不起作用。您必须使用以匿名过程作为输入的异步版本,然后在关闭对话框时在该过程中执行所需的操作。例如:

procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := False;
  MessageDlg('Do you really want to exit?', TMsgDlgType.mtConfirmation, [TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo], 0,
    procedure(const AResult: TModalResult)
    begin
      if AResult = mrYes then
        Application.Terminate;
    end
  );
end;

在Android中,按后退按钮不会触发该事件。要解决这个问题,我们必须更改后退按钮行为才能调用函数CloseQuery

procedure TFormMain.FormKeyUp(Sender: TObject; var Key: Word;  var KeyChar: Char; Shift: TShiftState);
begin
    if Key = vkHardwareBack then
    begin
        TPlatformServices.Current.SupportsPlatformService(IFMXVirtualKeyboardService, IInterface(FService));
        if (FService <> nil) and (TVirtualKeyboardState.Visible in FService.VirtualKeyBoardState) then
        begin
            // Back button pressed, keyboard visible, so do nothing...
        end else
        begin
            Key := 0;
            CloseQuery;
        end;
    end;
end;