如何在Delphi Form的OnClose事件中使用MessageDlg?

时间:2012-11-27 09:45:56

标签: delphi dialog delphi-xe2

点击“十字按钮”或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;

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

2 个答案:

答案 0 :(得分:8)

如果您输入

,窗口将保持打开状态

Action := caNone;

在你的其他部分

答案 1 :(得分:8)

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;