如何使用TTaskDialog?

And*_*and 26 delphi ttaskdialog

如何使用TTaskDialog该类(在Delphi 2009及更高版本中)?官方文档没有帮助.实际上,通过使用CodeInsight或VCL源代码检查类,您可以学到更多.那里没有教学解释,但至少也没有错误(嗯,只有几个).

就在最近,我想知道如何回应对话框中的超链接点击.实际上,设置tfEnableHyperlinks标志,您可以在对话框的文本部分中包含HTML超链接.(好吧,文档说的是标志:"如果设置,内容,页脚和扩展文本可以包含超链接."当然,使用<AHTML元素实现链接是"显而易见的" .)我设法弄清楚我自己使用该OnHyperLinkClick事件来响应超链接的点击.但是这个事件是一个TNotifyEvent,所以你怎么知道点击了什么链接?好吧,文档没有说明这一点,所以我不得不猜测.最终我发现URL对话框的公共属性已经设置好了,所以我可以做到

procedure TmainFrm.TaskDialogHyperLinkClicked(Sender: TObject);
begin
  if Sender is TTaskDialog then
    with Sender as TTaskDialog do
      ShellExecute(0, 'open', PChar(URL), nil, nil, SW_SHOWNORMAL);
end;
Run Code Online (Sandbox Code Playgroud)

官方文档说,关于这个属性:

URL包含"任务"对话框的URL.

现在,你必须承认,这是一个很好的解释!但它比这更糟糕:文档不仅缺乏解释,还包含错误.例如,

ExpandButtonCaption:此按钮的附加信息.

那不是很准确.什么按钮?如果你显示这个特定属性的帮助,它说

ExpandButtonCaption包含扩展标题时要显示的其他文本.

也不好.标题是什么?一个正确的解释是

ExpandButtonCaption是按钮旁边显示的文本,用户可以展开对话框以显示更多信息.例如,此属性可能是"更多详细信息".

无论如何,目前,我正在尝试使用两个命令链接按钮创建一个对话框.我知道操作系统可以显示带有标题和更长解释的这些按钮,但我似乎无法使用它来使其工作TTaskButton.文档不是很好.

但是,我不会在SO处询问如何实现这一特定事物,而是提出另一个问题:

是否有TTaskDialog类的任何(非官方)文档?

And*_*and 61

如果找不到文档,请写下:

任务对话的Hello World

with TTaskDialog.Create(Self) do
  try
    Caption := 'My Application';
    Title := 'Hello World!';
    Text := 'I am a TTaskDialog, that is, a wrapper for the Task Dialog introduced ' +
            'in the Microsoft Windows Vista operating system. Am I not adorable?';
    CommonButtons := [tcbClose];
    Execute;
  finally
    Free;
  end;
Run Code Online (Sandbox Code Playgroud)

Caption是窗口标题栏中显示的文本,Title是标题,Text是对话框的正文.不用说,Execute显示任务对话框,结果如下所示.(我们将CommonButtons在一两个部分返回酒店.)

TTaskDialog的示例

成为一个行为良好的公民

当然,如果在没有任务对话框API的Windows XP下运行,任务对话框将使程序崩溃.如果禁用视觉主题,它也将无效.在任何这种情况下,我们都需要坚持老式的MessageBox.因此,在实际应用中,我们需要这样做

if (Win32MajorVersion >= 6) and ThemeServices.ThemesEnabled then
  with TTaskDialog.Create(Self) do
    try
      Caption := 'My Application';
      Title := 'Hello World!';
      Text := 'I am a TTaskDialog, that is, a wrapper for the Task Dialog introduced ' +
              'in the Microsoft Windows Vista operating system. Am I not adorable?';
      CommonButtons := [tcbClose];
      Execute;
    finally
      Free;
    end
else
  MessageBox(Handle,
             'I am an ordinary MessageBox conveying the same message in order to support' +
             'older versions of the Microsoft Windows operating system (XP and below).',
             'My Application',
             MB_ICONINFORMATION or MB_OK);
Run Code Online (Sandbox Code Playgroud)

在本文的其余部分中,我们将假设向后兼容性的税收正在支付,而是单独专注于任务对话框.

对话类型.模态结果

所述CommonButtons属性的类型的TTaskDialogCommonButtons,被定义为

TTaskDialogCommonButton = (tcbOk, tcbYes, tcbNo, tcbCancel, tcbRetry, tcbClose);
TTaskDialogCommonButtons = set of TTaskDialogCommonButton;
Run Code Online (Sandbox Code Playgroud)

此属性确定对话框中显示的按钮(如果没有手动添加按钮,我们稍后会这样做).如果用户单击任何这些按钮,则相应的TModalResult值将在返回后ModalResult立即存储在属性中Execute.该MainIcon属性确定对话框中显示的图标,当然应该反映对话框的性质,按钮组也是如此.正式的整数,MainIcon可以被设置为任何值tdiNone,tdiWarning,tdiError,tdiInformation,和tdiShield.

with TTaskDialog.Create(Self) do
  try
    Caption := 'My Application';
    Title := 'The Process';
    Text := 'Do you want to continue even though [...]?';
    CommonButtons := [tcbYes, tcbNo];
    MainIcon := tdiNone; // There is no tdiQuestion
    if Execute then
      if ModalResult = mrYes then
        beep;
  finally
    Free;
  end;
Run Code Online (Sandbox Code Playgroud)

TTaskDialog的示例

以下是其余图标类型的示例(屏蔽,警告和错误):

TTaskDialog的示例

TTaskDialog的示例

TTaskDialog的示例

最后,您应该知道可以使用该DefaultButton属性在对话框中设置默认按钮.

with TTaskDialog.Create(Self) do
  try
    Caption := 'My Application';
    Title := 'The Process';
    Text := 'Do you want to continue even though [...]?';
    CommonButtons := [tcbYes, tcbNo];
    DefaultButton := tcbNo;
    MainIcon := tdiNone;
    if Execute then
      if ModalResult = mrYes then
        beep;
  finally
    Free;
  end;
Run Code Online (Sandbox Code Playgroud)

TTaskDialog的示例

自定义按钮

您可以将自定义按钮添加到任务对话框.实际上,您可以将CommonButtons属性设置为空集,并完全依赖于自定义按钮(以及无限数量的此类按钮).以下实际示例显示了这样一个对话框:

with TTaskDialog.Create(self) do
  try
    Title := 'Confirm Removal';
    Caption := 'Rejbrand BookBase';
    Text := Format('Are you sure that you want to remove the book file named "%s"?', [FNameOfBook]);
    CommonButtons := [];
    with TTaskDialogButtonItem(Buttons.Add) do
    begin
      Caption := 'Remove';
      ModalResult := mrYes;
    end;
    with TTaskDialogButtonItem(Buttons.Add) do
    begin
      Caption := 'Keep';
      ModalResult := mrNo;
    end;
    MainIcon := tdiNone;
    if Execute then
      if ModalResult = mrYes then
        DoDelete;
  finally
    Free;
  end
Run Code Online (Sandbox Code Playgroud)

TTaskDialog的示例

命令链接

任务对话框按钮可以是命令链接,而不是传统的按钮.这是通过设置tfUseCommandLinks标志(in Flags)来实现的.现在您还可以设置CommandLinkHint(按钮)属性:

with TTaskDialog.Create(self) do
  try
    Title := 'Confirm Removal';
    Caption := 'Rejbrand BookBase';
    Text := Format('Are you sure that you want to remove the book file named "%s"?', [FNameOfBook]);
    CommonButtons := [];
    with TTaskDialogButtonItem(Buttons.Add) do
    begin
      Caption := 'Remove';
      CommandLinkHint := 'Remove the book from the catalogue.';
      ModalResult := mrYes;
    end;
    with TTaskDialogButtonItem(Buttons.Add) do
    begin
      Caption := 'Keep';
      CommandLinkHint := 'Keep the book in the catalogue.';
      ModalResult := mrNo;
    end;
    Flags := [tfUseCommandLinks];
    MainIcon := tdiNone;
    if Execute then
      if ModalResult = mrYes then
        DoDelete;
  finally
    Free;
  end
Run Code Online (Sandbox Code Playgroud)

TTaskDialog的示例

tfAllowDialogCancellation标志将恢复关闭系统菜单项(和标题栏按钮 - 实际上,它将恢复整个系统菜单).

TTaskDialog的示例

不要在最终用户处抛出技术细节

您可以使用属性ExpandedTextExpandedButtonCaption添加一段文本(前者),该文本仅在用户单击按钮(位于后一属性中的文本左侧)之后显示以请求它.

with TTaskDialog.Create(self) do
  try
    Title := 'Confirm Removal';
    Caption := 'Rejbrand BookBase';
    Text := Format('Are you sure that you want to remove the book file named "%s"?', [FNameOfBook]);
    CommonButtons := [];
    with TTaskDialogButtonItem(Buttons.Add) do
    begin
      Caption := 'Remove';
      CommandLinkHint := 'Remove the book from the catalogue.';
      ModalResult := mrYes;
    end;
    with TTaskDialogButtonItem(Buttons.Add) do
    begin
      Caption := 'Keep';
      CommandLinkHint := 'Keep the book in the catalogue.';
      ModalResult := mrNo;
    end;
    Flags := [tfUseCommandLinks, tfAllowDialogCancellation];
    ExpandButtonCaption := 'Technical information';
    ExpandedText := 'If you remove the book item from the catalogue, the corresponding *.book file will be removed from the file system.';
    MainIcon := tdiNone;
    if Execute then
      if ModalResult = mrYes then
        DoDelete;
  finally
    Free;
  end
Run Code Online (Sandbox Code Playgroud)

下图显示了用户单击按钮以显示其他详细信息后的对话框.

TTaskDialog的示例

如果添加tfExpandFooterArea标记,则会在页脚中显示其他文本:

TTaskDialog的示例

在任何情况下,您都可以通过添加tfExpandedByDefault标志让对话框打开,并显示已展开的详细信息.

自定义图标

您可以在任务对话框中使用任何自定义图标,方法是使用该tfUseHiconMain标志并指定TIcon要在CustomMainIcon属性中使用.

with TTaskDialog.Create(self) do
  try
    Caption := 'About Rejbrand BookBase';
    Title := 'Rejbrand BookBase';
    CommonButtons := [tcbClose];
    Text := 'File Version: ' + GetFileVer(Application.ExeName) + #13#10#13#10'Copyright © 2011 Andreas Rejbrand'#13#10#13#10'http://english.rejbrand.se';
    Flags := [tfUseHiconMain, tfAllowDialogCancellation];
    CustomMainIcon := Application.Icon;
    Execute;
  finally
    Free;
  end
Run Code Online (Sandbox Code Playgroud)

TTaskDialog的示例

超链接

你甚至可以使用类似HTML的超链接对话框中(中Text,Footer,ExpandedText),如果你只加了tfEnableHyperlinks标志:

with TTaskDialog.Create(self) do
  try
    Caption := 'About Rejbrand BookBase';
    Title := 'Rejbrand BookBase';
    CommonButtons := [tcbClose];
    Text := 'File Version: ' + GetFileVer(Application.ExeName) + #13#10#13#10'Copyright © 2011 Andreas Rejbrand'#13#10#13#10'<a href="http://english.rejbrand.se">http://english.rejbrand.se</a>';
    Flags := [tfUseHiconMain, tfAllowDialogCancellation, tfEnableHyperlinks];
    CustomMainIcon := Application.Icon;
    Execute;
  finally
    Free;
  end
Run Code Online (Sandbox Code Playgroud)

TTaskDialog的示例

但请注意,单击链接时没有任何反应.链接的操作必须手动实现,当然 - 这是一件好事.要做到这一点,回应OnHyperlinkClicked事件,这是一个TNotifyEvent.链接的URL(即a元素的href)存储在以下URL公共属性中TTaskDialog:

procedure TForm1.TaskDialogHyperLinkClicked(Sender: TObject);
begin
  if Sender is TTaskDialog then
    with Sender as TTaskDialog do
      ShellExecute(0, 'open', PChar(URL), nil, nil, SW_SHOWNORMAL);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  with TTaskDialog.Create(self) do
    try
      Caption := 'About Rejbrand BookBase';
      Title := 'Rejbrand BookBase';
      CommonButtons := [tcbClose];
      Text := 'File Version: ' + GetFileVer(Application.ExeName) + #13#10#13#10'Copyright © 2011 Andreas Rejbrand'#13#10#13#10'<a href="http://english.rejbrand.se">http://english.rejbrand.se</a>';
      Flags := [tfUseHiconMain, tfAllowDialogCancellation, tfEnableHyperlinks];
      OnHyperlinkClicked := TaskDialogHyperlinkClicked;
      CustomMainIcon := Application.Icon;
      Execute;
    finally
      Free;
    end
end;
Run Code Online (Sandbox Code Playgroud)

页脚

您可以使用FooterFooterIcon属性来创建页脚.该icon属性接受与该属性相同的值MainIcon.

with TTaskDialog.Create(self) do
  try
    Caption := 'My Application';
    Title := 'A Question';
    Text := 'This is a really tough one...';
    CommonButtons := [tcbYes, tcbNo];
    MainIcon := tdiNone;
    FooterText := 'If you do this, then ...';
    FooterIcon := tdiWarning;
    Execute;
  finally
    Free;
  end
Run Code Online (Sandbox Code Playgroud)

TTaskDialog的示例

使用tfUseHiconFooter标志和CustomFooterIcon属性,您可以使用页脚中的任何自定义图标,方法与选择自己的主图标的方式相同.

一个复选框

使用VerificationText字符串属性,可以在任务对话框的页脚中添加一个复选框.复选框的标题是属性.

用TTaskDialog.Create(self)做尝试Caption:='我的应用程序'; 标题:='一个问题'; 文字:='这是一个非常艰难的......'; CommonButtons:= [tcbYes,tcbNo]; MainIcon:= tdiNone; VerificationText:='记住我的选择'; 执行; 终于自由了; 结束

TTaskDialog的示例

您可以通过指定tfVerificationFlagChecked标志来初始选中复选框.不幸的是,由于VCL实现中的错误(?),返回TTaskDialog时包含此标志Execute并不反映复选框的最终状态.为了跟踪复选框,应用程序因此需要记住初始状态并切换内部标志作为对每个OnVerificationClicked事件的响应,每当在对话框的模态期间改变复选框的状态时触发该事件.

单选按钮

单选按钮的实现方式类似于添加自定义按钮(或命令链接按钮)的方式:

with TTaskDialog.Create(self) do
  try
    Caption := 'My Application';
    Title := 'A Question';
    Text := 'This is a really tough one...';
    CommonButtons := [tcbOk, tcbCancel];
    MainIcon := tdiNone;
    with RadioButtons.Add do
      Caption := 'This is one option';
    with RadioButtons.Add do
      Caption := 'This is another option';
    with RadioButtons.Add do
      Caption := 'This is a third option';
    if Execute then
      if ModalResult = mrOk then
        ShowMessage(Format('You chose %d.', [RadioButton.Index]));
  finally
    Free;
  end
Run Code Online (Sandbox Code Playgroud)

TTaskDialog的示例

  • Embarcadero应该使用您的代码并使用免费许可证奖励您. (3认同)
  • 这是有史以来最好的答案。我想宣布您为 StackOverflow 荣誉公民。 (3认同)
  • 好的写作.+1额外的努力. (2认同)

Leo*_*era 6

这是旧的东西,但我在这里添加它是为了完整性:

用于XP,Vista,Seven的开源SynTaskDialog单元

TTaskDialog 在XP下运行的单元(使用VCL),但在Vista +下使用系统TaskDialog.


Chr*_*ton 5

TMS有一个不错的包装器,并且在XP上运行时还可以模拟新行为。轻而易举。它不是免费的,并且不能真正回答您的“如何做”问题。

http://www.tmssoftware.com/site/vtd.asp

他们也有一些文章讨论对话框,如果您想自己包装,有一些源代码可能对您有用。

http://www.tmssoftware.com/site/atbdev5.asp

http://www.tmssoftware.com/site/atbdev7.asp