delphi – 如何使用TTaskDialog?
有没有关于TTaskDialog类的文档(在Delphi 2009及更高版本中)?
The official documentation是,如果你原谅我的法语,毫无价值。实际上,您可以通过使用CodeInsight或VCL源代码检查该类来学习更多。那里没有教学性的解释,但至少没有任何错误(呃,
just a few)。
而刚才我想知道如何在对话框中回应超链接点击。实际上,设置tfEnableHyperlinks标志,您可以在对话框的文本部分中包含HTML超链接。 (那么这个文档就是关于这个标志的:“如果设置,内容,页脚和扩展文本可以包含超链接。”当然,使用< A 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,SW_SHOWNORMAL); end; 官方文件说,关于这个属性:
现在你必须承认,这是一个很好的解释!但是比这更糟糕:文档不仅没有解释,还包含错误。 For instance,
这不是很准确。什么按钮?如果您显示此特定属性的帮助,it says
也不好什么标题?一个适当的解释是
无论如何,目前我正在尝试使用两个命令链接按钮创建一个对话框。我知道操作系统可以使用标题和更长的解释来显示这些按钮,但是我似乎无法使用TTaskButton使其工作。文档isn’t great。 但是,在这个SO的这个特殊的事情上,我不会问这个特殊的事情,我会问另外一个问题:
解决方法
如果找不到文档,那么
write it:
任务对话框的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; 标题是窗口标题栏中显示的文字,标题是标题,文字是对话框的正文。不用说,Execute显示任务对话框,结果如下所示。 (我们将在一两节内返回到CommonButtons属性。) Sample of a TTaskDialog http://specials.rejbrand.se/TTaskDialog/taskdialog1.png 作为一个很好的公民 当然,如果在Windows XP下运行,任务对话框将会崩溃,在那里没有任务对话框API。如果视觉主题被禁用,它也将不起作用。在任何这种情况下,我们需要坚持使用老式的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,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); 在本文的其余部分,我们将假设tax的向后兼容性正在付费,而是专注于任务对话框。 对话框类型模态结果 CommonButtons属性的类型为TTaskDialogCommonButtons,定义为 TTaskDialogCommonButton = (tcbOk,tcbYes,tcbNo,tcbCancel,tcbRetry,tcbClose); TTaskDialogCommonButtons = set of TTaskDialogCommonButton; 此属性确定对话框中显示的按钮(如果没有手动添加按钮,我们稍后将会这样做)。如果用户点击任何这些按钮,则一旦Execute返回,相应的TModalResult值将被存储在ModalResult属性中。 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; Sample of a TTaskDialog http://specials.rejbrand.se/TTaskDialog/taskdialog2.png 以下是剩余图标类型(屏蔽,警告和错误)的示例: Sample of a TTaskDialog http://specials.rejbrand.se/TTaskDialog/taskdialog3.png Sample of a TTaskDialog http://specials.rejbrand.se/TTaskDialog/taskdialog4.png Sample of a TTaskDialog http://specials.rejbrand.se/TTaskDialog/taskdialog5.png 最后,您应该知道可以使用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; Sample of a TTaskDialog http://specials.rejbrand.se/TTaskDialog/taskdialog6.png 自定义按钮 您可以将自定义按钮添加到任务对话框中。实际上,您可以将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 http://specials.rejbrand.se/TTaskDialog/taskdialog7.png 命令链接 而不是古典按钮,任务对话框按钮可以是命令链接。这通过设置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 http://specials.rejbrand.se/TTaskDialog/taskdialog8.png tfAllowDialogCancellation标志将恢复关闭系统菜单项(和标题栏按钮 – 实际上,它将恢复整个系统菜单)。 Sample of a TTaskDialog http://specials.rejbrand.se/TTaskDialog/taskdialog9.png 不要在最终用户上投掷技术细节 您可以使用属性ExpandedText和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 http://specials.rejbrand.se/TTaskDialog/taskdialog10.png 如果您添加了tfExpandFooterArea标志,则附加文本将显示在页脚中: Sample of a TTaskDialog http://specials.rejbrand.se/TTaskDialog/taskdialog11.png 在任何情况下,您可以通过添加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 Sample of a TTaskDialog http://specials.rejbrand.se/TTaskDialog/taskdialog12.png 超链接 如果只添加tfEnableHyperlinks标志,您甚至可以在对话框中使用类似HTML的超链接(在Text,Footer和ExpandedText中) 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 http://specials.rejbrand.se/TTaskDialog/taskdialog13.png 但请注意,当您单击链接时不会发生任何情况。链接的动作必须手动实现,当然这是一件好事。要做到这一点,请回应OnTypotelClicked事件,这是一个TNotifyEvent。链接的URL(a元素的href,即)是存储在TTaskDialog的URL公共属性中: procedure TForm1.TaskDialogHyperLinkClicked(Sender: TObject); begin if Sender is TTaskDialog then with Sender as TTaskDialog do ShellExecute(0,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,tfEnableHyperlinks]; OnHyperlinkClicked := TaskDialogHyperlinkClicked; CustomMainIcon := Application.Icon; Execute; finally Free; end end; 页脚 您可以使用Footer和FooterIcon属性创建页脚。该图标属性接受与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 Sample of a TTaskDialog http://specials.rejbrand.se/TTaskDialog/taskdialog14.png 使用tfUseHiconFooter标志和CustomFooterIcon属性,您可以在页脚中使用任何自定义图标,方式与您可以选择自己的主图标相同。 复选框 使用VerificationText字符串属性,可以在任务对话框的页脚中添加一个复选框。该复选框的标题是属性。 用TTaskDialog.Create(self)做 Sample of a TTaskDialog http://specials.rejbrand.se/TTaskDialog/taskdialog15.png 您可以通过指定tfVerificationFlagChecked标志来最初检查该复选框。不幸的是,由于TTaskDialog的VCL实现中有一个bug(?),当Execute已经返回时,包含这个标志并不能反映复选框的最终状态。为了跟踪复选框,应用程序因此需要记住初始状态,并将内部标志切换为对每个OnVerificationClicked事件的响应,每次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 a TTaskDialog http://specials.rejbrand.se/TTaskDialog/taskdialog16.png (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |