加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – TTaskDialog.Execute总是返回True,甚至点击取消

发布时间:2020-12-15 09:36:37 所属栏目:大数据 来源:网络整理
导读:在Delphi XE2 / XE3中执行以下代码 with TTaskDialog.Create(Self) do begin try if Execute then ShowMessage('Success') else ShowMessage('Failed'); finally Free; end;end; 无论您点击什么按钮关闭对话框,显示的消息始终是成功. Delphi文档写成TTaskDia
在Delphi XE2 / XE3中执行以下代码

with TTaskDialog.Create(Self) do begin
  try
    if Execute then
      ShowMessage('Success')
    else
      ShowMessage('Failed');
  finally
    Free;
  end;
end;

无论您点击什么按钮关闭对话框,显示的消息始终是成功.

Delphi文档写成TTaskDialog.Execute为

Use Execute to display the Task Dialog. Execute opens the
task-selection dialog,returning true when the user selects a task and
clicks Open. If the user clicks Cancel,Execute returns false.

解决方法

看来文档不正确,这是TTaskDialog.Execute方法的执行流程:

TTaskDialog.Execute -> TCustomTaskDialog.Execute ->
TCustomTaskDialog.DoExecute -> TaskDialogIndirect = S_OK?

如您所见,只有当TaskDialogIndirect函数返回S_OK时,方法的结果才为真.

要评估对话框的结果,必须使用ModalResult属性.

with TTaskDialog.Create(Self) do
  begin
    try
      if Execute then
        case ModalResult of
         mrYes    : ShowMessage('Success');
         mrCancel : ShowMessage('Cancel');
        else
         ShowMessage('Another button was pressed');
        end;
    finally
      Free;
    end;
  end;

注意:如果使用关闭按钮关闭对话框,则会在ModalResult属性中返回mrCancel值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读