You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现用户关闭程序时弹出确认提示?Delphi代码异常排查

代码问题定位
  • 返回值判断错误:Delphi 中MessageDlg的返回值是按钮对应常量,点击「是(mbYes)」返回mrYes(数值为6),你写的判断条件= 0永远不会触发,Application.Terminate代码从未执行。
  • 未拦截默认关闭逻辑:FormClose事件的Action参数默认会执行关闭动作,你没有在用户点击取消时将Action设为caNone,所以无论选择哪个按钮,窗体都会按默认逻辑关闭,主窗体关闭后程序自然直接退出。
修复后代码
procedure TfrmLogin.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  // 只有用户点击确认退出时才走默认关闭逻辑,否则拦截关闭
  if MessageDlg('Are you sure you want to exit?', mtWarning, [mbYes, mbCancel], 0) <> mrYes then
  begin
    Action := caNone;
  end;
  // 如果当前是主窗体,不需要额外调用Application.Terminate,窗体默认关闭就会自动退出程序
end;

内容的提问来源于stack exchange,提问作者Michael

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 04:45:01