3

当通过单击“交叉按钮”或 Alt + F4 关闭我的表单时,我希望用户询问他是否要关闭应用程序。如果是,我将终止申请,否则无事可做。我在表单的 onclose 事件上使用以下代码

procedure MyForm.FormClose(Sender: TObject; var Action: TCloseAction);
var
  buttonSelected : integer;
begin
  buttonSelected := MessageDlg('Do you really want to close the application?',mtCustom, [mbYes,mbNo], 0);
  if buttonSelected = mrYES then
  begin
    Application.Terminate;
  end
  else
  begin
    //What should I write here to resume the application
  end;

end;

无论我单击是或否,我的应用程序都将终止。我应该怎么做才能在没有点击确认框时,我的应用程序不应该终止。我应该如何改进我的上述功能?我是否使用正确的表单事件来实现此功能?请帮忙..

4

2 回答 2

9

如果您键入,窗口将保持打开状态

Action := caNone;

在你的其他部分

于 2012-11-27T09:53:47.923 回答
9
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var
  buttonSelected: integer;
begin
  buttonSelected := MessageDlg('Do you really want to close the application?', mtCustom, [mbYes, mbNo], 0);
  if buttonSelected = mrYES then
  begin
    CanClose:=true;
  end
  else
  begin
    CanClose:=false;
  end;
end;

或如@TLama 建议的那样,简化:

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := MessageDlg('Do you really want to close the application?', mtCustom, [mbYes, mbNo], 0) = mrYES;
end;
于 2012-11-27T09:54:12.400 回答