如何使用TTaskDialog?

发布于 2024-10-17 06:31:02 字数 1917 浏览 9 评论 0 原文

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

就在最近,我想知道如何响应对话框中的超链接点击。事实上,通过设置 tfEnableHyperlinks 标志,您可以在对话框的文本部分中包含 HTML 超链接。 (嗯,文档关于该标志是这样说的:“如果设置了,内容、页脚和扩展文本可以包含超链接。”当然,“显然”链接是使用 实现的HTML 元素。)我自己发现,您使用 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;

官方文档所述,关于此属性:

URL 包含任务的 URL 对话框。

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

ExpandButtonCaption:此按钮的附加信息。

这不太准确。什么按钮?如果您显示此特定属性的帮助,它会显示

ExpandButtonCaption 包含展开标题时要显示的附加文本。

也不好。什么字幕?正确的解释是

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

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

但我不会问如何在 SO 实现这一特定目标,而是问另一个问题:

TTaskDialog 类有任何(非官方)文档吗?

How does one use the TTaskDialog class (in Delphi 2009 and later)? The official documentation hasn't helped. In fact, you learn much more by examining the class using CodeInsight or the VCL source code. There are no pedagogical explanations there, but at least there are no errors either (well, just a few).

And just recently, I wondered how you could respond to hyperlink clicks in the dialog. Indeed, setting the tfEnableHyperlinks flag, you can include HTML hyperlinks in the dialog's textual parts. (Well, the doc's said about the flag: "If set, content, footer, and expanded text can include hyperlinks." Naturally, it is "obvious" that the links are implemented using the <A HTML element.) And I managed to figure out myself that you use the OnHyperLinkClick event to respond to clicks on hyperlinks. But this event is a TNotifyEvent, so how do you know what link was clicked? Well, the documentation said nothing about that, so I had to guess. Eventually I found out that the URL public property of the dialog is set, so I could do

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;

The official documentation says, regarding this property:

URL contains the URL for the Task
Dialog.

Now, you have to admit, that's a great explanation! But it is worse than this: not only does the documentation lack in explanations, it also contains errors. For instance,

ExpandButtonCaption: Additional information for this button.

That's not very accurate. What button? If you show the help for this particular property, it says

ExpandButtonCaption contains additional text to be displayed when the caption is expanded.

Not good either. What caption? A proper explanation would be

ExpandButtonCaption is the text shown next to the button that lets the user expand the dialog to make it show more information. For instance, this property might be "More details".

At any rate, currently, I am trying to create a dialog with two command-link buttons. I know that the operating system can display these buttons with both a caption and a longer explanation, but I seem not to be able to make it work using the TTaskButton. The documentation isn't great.

But instead of asking how to achieve this particular thing here at SO, I will ask another question:

Is there any (unofficial) documentation for the TTaskDialog class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半世晨晓 2024-10-24 06:31:02

如果您找不到文档,请编写它

任务对话框的 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;

Caption 是窗口标题栏中显示的文本,Title 是标题,Text 是对话框的正文。不用说,执行会显示任务对话框,结果如下所示。 (我们将在一两节中返回到 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);

在本文的其余部分中,我们将假设,而是仅专注于任务对话框。

对话框的类型。模态结果

CommonButtons 属性的类型为 TTaskDialogCommonButtons,定义为

TTaskDialogCommonButton = (tcbOk, tcbYes, tcbNo, tcbCancel, tcbRetry, tcbClose);
TTaskDialogCommonButtons = set of TTaskDialogCommonButton;

该属性确定对话框中显示的按钮(如果没有手动添加按钮,我们稍后会这样做) 。如果用户单击其中任何按钮,则在 Execute 返回后,相应的 TModalResult 值将存储在 ModalResult 属性中。 MainIcon 属性确定对话框中显示的图标,并且当然应该反映对话框的性质,按钮组也应该如此。 MainIcon 形式上是一个整数,可以设置为 tdiNonetdiWarningtdiError 中的任何值tdiInformationtdiShield

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;

TTaskDialog 的示例

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

< img src="https://specials.rejbrand.se/TTaskDialog/taskdialog3.png" alt="TTaskDialog 示例">

TTaskDialog 的示例

TTaskDialog 的示例

最后,您应该知道可以使用 < code>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;

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

Sample of a TTaskDialog

命令

链接任务对话框按钮可以是命令链接。这是通过设置 tfUseCommandLinks 标志(在 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

Sample of a TTaskDialog

tfAllowDialogCancellation 标志将恢复关闭的系统菜单项(和标题栏按钮——事实上,它将恢复整个系统菜单)。

TTaskDialog 示例

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

您可以使用属性 ExpandedText< /code> 和 ExpandedButtonCaption 添加一段文本(前者),仅在用户单击按钮(后一个属性中文本的左侧)请求它后才显示。

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

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

Sample of a TTaskDialog

如果添加 tfExpandFooterArea 标志,则附加文本将改为页脚中显示:

Sample of a TTaskDialog

在任何情况下,您都可以打开对话框,其中的详细信息已展开通过添加 tfExpandedByDefault 标志。

自定义图标

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

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

TTaskDialog 示例

超链接

您甚至可以在对话框中使用类似 HTML 的超链接(在 TextFooter、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

Sample of a TTaskDialog

但是请注意,单击该链接时没有任何反应。链接的操作必须手动实现,这当然是一件好事。为此,请响应 OnHyperlinkClicked 事件,该事件是 TNotifyEvent。链接的 URL(即 a 元素的 href)存储在 TTaskDialogURL 公共属性中:

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;

页脚

您可以使用 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

TTaskDialog 示例

使用 tfUseHiconFooter 标志和 CustomFooterIcon 属性,您可以在页脚中使用任何自定义图标,就像您可以选择自己的主图标一样。

复选框

使用 VerificationText 字符串属性,您可以将复选框添加到任务对话框的页脚。复选框的标题是属性。

with TTaskDialog.Create(self) do
  try
    Caption := 'My Application';
    Title := 'A Question';
    Text := 'This is a really tough one...';
    CommonButtons := [tcbYes, tcbNo];
    MainIcon := tdiNone;
    VerificationText := 'Remember my choice';
    Execute;
  finally
    Free;
  end

TTaskDialog 示例

您可以通过指定 tfVerificationFlagChecked 标志来使复选框最初处于选中状态。不幸的是,由于 TTaskDialog 的 VCL 实现中存在错误 (?),当 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

Sample of一个 TTaskDialog

If you can't find the documentation, then write it:

The Hello World of a Task Dialog

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;

Caption is the text shown in the titlebar of the window, Title is the header, and Text is the body matter of the dialog. Needless to say, Execute displays the task dialog, and the result is shown below. (We will return to the CommonButtons property in a section or two.)

Sample of a TTaskDialog

Being a Well-Behaved Citizen

Of course, the task dialog will crash the program if running under Windows XP, where there is not task dialog API. It will also not work if visual themes are disabled. In any such case, we need to stick to the old-fashioned MessageBox. Hence, in a real application, we would need to do

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);

In the rest of this article, we will assume that the tax of backwards compatibility is being payed, and instead concentrate on the task dialog alone.

Types of Dialogs. Modal Results

The CommonButtons property is of type TTaskDialogCommonButtons, defined as

TTaskDialogCommonButton = (tcbOk, tcbYes, tcbNo, tcbCancel, tcbRetry, tcbClose);
TTaskDialogCommonButtons = set of TTaskDialogCommonButton;

This property determines the buttons shown in the dialog (if no buttons are added manually, as we will do later on). If the user clicks any of these buttons, the corresponding TModalResult value will be stored in the ModalResult property as soon as Execute has returned. The MainIcon property determines the icon shown in the dialog, and should -- of course -- reflect the nature of the dialog, as should the set of buttons. Formally an integer, MainIcon can be set to any of the values tdiNone, tdiWarning, tdiError, tdiInformation, and 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;

Sample of a TTaskDialog

Below are samples of the remaining icon types (shield, warning, and error, respectively):

Sample of a TTaskDialog

Sample of a TTaskDialog

Sample of a TTaskDialog

Finally, you should know that you can use the DefaultButton property to set the default button in the dialog box.

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;

Sample of a TTaskDialog

Custom Buttons

You can add custom buttons to a task dialog. In fact, you can set the CommonButtons property to the empty set, and rely entirely on custom buttons (and un unlimited number of such buttons, too). The following real-world example shows such a dialog box:

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

Sample of a TTaskDialog

Command Links

Instead of classical pushbuttons, the task dialog buttons can be command links. This is achieved by setting the tfUseCommandLinks flag (in Flags). Now you can also set the CommandLinkHint (per-button) property:

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

Sample of a TTaskDialog

The tfAllowDialogCancellation flag will restore the close system menu item (and titlebar button -- in fact, it will restore the entire system menu).

Sample of a TTaskDialog

Don't Throw Technical Details at the End User

You can use the properties ExpandedText and ExpandedButtonCaption to add a piece of text (the former) that is only displayed after the user clicks a button (to the left of the text in the latter property) to request it.

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

The image below shows the dialog after the user has clicked the button to reveal the additional details.

Sample of a TTaskDialog

If you add the tfExpandFooterArea flag, the additional text will instead be shown in the footer:

Sample of a TTaskDialog

In any case, you can let the dialog open with the details already expanded by adding the tfExpandedByDefault flag.

Custom Icons

You can use any custom icon in a task dialog, by using the tfUseHiconMain flag and specifying the TIcon to use in the CustomMainIcon property.

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

Sample of a TTaskDialog

Hyperlinks

You can even use HTML-like hyperlinks in the dialog (in Text, Footer, and ExpandedText), if you only add the tfEnableHyperlinks flag:

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

Sample of a TTaskDialog

Notice, however, that nothing happens when you click the link. The action of the link must be implemented manually, which -- of course -- is a good thing. To do this, respond to the OnHyperlinkClicked event, which is a TNotifyEvent. The URL of the link (the href of the a element, that is) is stored in the URL public property of the 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;

The Footer

You can use the Footer and FooterIcon properties to create a footer. The icon property accepts the same values as the MainIcon property.

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

Sample of a TTaskDialog

Using the tfUseHiconFooter flag and the CustomFooterIcon property, you can use any custom icon in the footer, in the same way as you can choose your own main icon.

A Checkbox

Using the VerificationText string property, you can add a checkbox to the footer of the task dialog. The caption of the checkbox is the property.

with TTaskDialog.Create(self) do
  try
    Caption := 'My Application';
    Title := 'A Question';
    Text := 'This is a really tough one...';
    CommonButtons := [tcbYes, tcbNo];
    MainIcon := tdiNone;
    VerificationText := 'Remember my choice';
    Execute;
  finally
    Free;
  end

Sample of a TTaskDialog

You can make the checkbox initially checked by specifying the tfVerificationFlagChecked flag. Unfortunately, due to a bug (?) in the VCL implementation of the TTaskDialog, the inclusion of this flag when Execute has returned doesn't reflect the final state of the checkbox. To keep track of the checkbox, the application thus needs to remember the initial state and toggle an internal flag as a response to each OnVerificationClicked event, which is triggered every time the state of the checkbox is changed during the modality of the dialog.

Radio Buttons

Radio buttons can be implemented in a way resembling how you add custom push buttons (or command link buttons):

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

Sample of a TTaskDialog

最偏执的依靠 2024-10-24 06:31:02

这是旧内容,但为了完整性,我在此处添加此内容:

适用于 XP,Vista,7 的开源 SynTaskDialog 单元

TTaskDialog 单元在 XP(使用 VCL)下工作,但在 Vista+ 下使用系统 TaskDialog。

This is old stuff, but I'm adding this here for completeness:

Open Source SynTaskDialog unit for XP,Vista,Seven

TTaskDialog unit that works under XP (with VCL), but uses the system TaskDialog under Vista+.

三人与歌 2024-10-24 06:31:02

TMS 有一个很好的包装器,并且它还模拟在 XP 上运行时的新行为。这是一个快速插入。但它不是免费的,并且不能真正回答您的“如何”问题。

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

他们还有一些他们讨论对话框的文章,如果您想制作自己的包装器,那么一些源代码可能对您有用。

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

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

TMS Has a nice wrapper, and it also emulates the new behavior when run on XP. It's a snap to drop-in. It's not free though, and doesn't really answer your "how to" question.

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

They also have some articles where they discuss the dialog, and there's some source code that may be useful to you if you want to make your own wrapper.

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

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

心凉怎暖 2024-10-24 06:31:02

这是我的简短文档:

  1. 不要使用它,除非您不希望您的应用程序在 XP 上运行。

  2. 像其他人一样,对 delphi doc wiki 内容提出改进建议,但是请注意短语“注意:任务对话框需要 Vista 或 Windows 7”。这是“不要使用它!”的代码。基本上,有人想到了完全支持新的 Windows Vista 对话框,其实现方式是编写仅调用对话框 API 的包装器代码。由于没有向您提供后备功能,因此您在 XP 上运气不好。

Here's my short documentation:

  1. Don't use it, unless you don't want your application to work on XP.

  2. Suggest improvements to the delphi doc wiki content, as others did, but note the phrase "Note: Task Dialogs require Vista or Windows 7". that's code for "Don't use it!". Basically, someone got the idea to fully support new Windows Vista dialogs, and the way it was done, was to write wrapper code that only calls the dialog APIs. Since no fallback functionality is provided to you, you are out of luck on XP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文