如何使用 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?
发布评论
评论(4)
如果您找不到文档,请编写它:
任务对话框的 Hello World
Caption
是窗口标题栏中显示的文本,Title
是标题,Text
是对话框的正文。不用说,执行
会显示任务对话框,结果如下所示。 (我们将在一两节中返回到CommonButtons
属性。)做一个有礼貌的公民
当然,如果在没有任务对话框 API 的 Windows XP 下运行,任务对话框会使程序崩溃。如果视觉主题被禁用,它也将不起作用。在任何这种情况下,我们都需要坚持使用老式的
MessageBox
。因此,在实际应用程序中,我们需要执行以下操作:在本文的其余部分中,我们将假设税,而是仅专注于任务对话框。
对话框的类型。模态结果
CommonButtons
属性的类型为TTaskDialogCommonButtons
,定义为该属性确定对话框中显示的按钮(如果没有手动添加按钮,我们稍后会这样做) 。如果用户单击其中任何按钮,则在
Execute
返回后,相应的TModalResult
值将存储在ModalResult
属性中。 MainIcon 属性确定对话框中显示的图标,并且当然应该反映对话框的性质,按钮组也应该如此。MainIcon
形式上是一个整数,可以设置为tdiNone
、tdiWarning
、tdiError
、中的任何值tdiInformation
和tdiShield
。以下是其余图标类型的示例(分别为屏蔽、警告和错误):
< img src="https://specials.rejbrand.se/TTaskDialog/taskdialog3.png" alt="TTaskDialog 示例">
最后,您应该知道可以使用 < code>DefaultButton 属性设置对话框中的默认按钮。
自定义按钮
您可以将自定义按钮添加到任务对话框。事实上,您可以将 CommonButtons 属性设置为空集,并完全依赖自定义按钮(并且此类按钮的数量也不受限制)。以下实际示例显示了这样一个对话框:
命令
链接任务对话框按钮可以是命令链接。这是通过设置
tfUseCommandLinks
标志(在Flags
中)来实现的。现在您还可以设置CommandLinkHint
(每个按钮)属性:tfAllowDialogCancellation
标志将恢复关闭的系统菜单项(和标题栏按钮——事实上,它将恢复整个系统菜单)。不要向最终用户抛出技术细节
您可以使用属性
ExpandedText< /code> 和
ExpandedButtonCaption
添加一段文本(前者),仅在用户单击按钮(后一个属性中文本的左侧)请求它后才显示。下图显示了用户单击按钮以显示其他详细信息后的对话框。
如果添加
tfExpandFooterArea
标志,则附加文本将改为页脚中显示:在任何情况下,您都可以打开对话框,其中的详细信息已展开通过添加 tfExpandedByDefault 标志。
自定义图标
您可以在任务对话框中使用任何自定义图标,方法是使用
tfUseHiconMain
标志并指定要在CustomMainIcon
属性中使用的TIcon
。超链接
您甚至可以在对话框中使用类似 HTML 的超链接(在
Text、
Footer、
和ExpandedText
),如果您仅添加tfEnableHyperlinks
标志:但是请注意,单击该链接时没有任何反应。链接的操作必须手动实现,这当然是一件好事。为此,请响应
OnHyperlinkClicked
事件,该事件是TNotifyEvent
。链接的 URL(即 a 元素的 href)存储在TTaskDialog
的URL
公共属性中:页脚
您可以使用
Footer
和FooterIcon
属性用于创建页脚。icon
属性接受与MainIcon
属性相同的值。使用
tfUseHiconFooter
标志和CustomFooterIcon
属性,您可以在页脚中使用任何自定义图标,就像您可以选择自己的主图标一样。复选框
使用
VerificationText
字符串属性,您可以将复选框添加到任务对话框的页脚。复选框的标题是属性。您可以通过指定
tfVerificationFlagChecked
标志来使复选框最初处于选中状态。不幸的是,由于TTaskDialog
的 VCL 实现中存在错误 (?),当Execute
返回时包含此标志并不反映复选框的最终状态。为了跟踪复选框,应用程序需要记住初始状态并切换内部标志作为对每个OnVerificationClicked
事件的响应,该事件在每次复选框状态更改时都会触发。对话的形式。单选按钮
单选按钮的实现方式与添加自定义按钮(或命令链接按钮)类似:
If you can't find the documentation, then write it:
The Hello World of a Task Dialog
Caption
is the text shown in the titlebar of the window,Title
is the header, andText
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 theCommonButtons
property in a section or two.)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 doIn 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 typeTTaskDialogCommonButtons
, defined asThis 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 theModalResult
property as soon asExecute
has returned. TheMainIcon
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 valuestdiNone
,tdiWarning
,tdiError
,tdiInformation
, andtdiShield
.Below are samples of the remaining icon types (shield, warning, and error, respectively):
Finally, you should know that you can use the
DefaultButton
property to set the default button in the dialog box.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:Command Links
Instead of classical pushbuttons, the task dialog buttons can be command links. This is achieved by setting the
tfUseCommandLinks
flag (inFlags
). Now you can also set theCommandLinkHint
(per-button) property:The
tfAllowDialogCancellation
flag will restore the close system menu item (and titlebar button -- in fact, it will restore the entire system menu).Don't Throw Technical Details at the End User
You can use the properties
ExpandedText
andExpandedButtonCaption
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.The image below shows the dialog after the user has clicked the button to reveal the additional details.
If you add the
tfExpandFooterArea
flag, the additional text will instead be shown in the footer: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 theTIcon
to use in theCustomMainIcon
property.Hyperlinks
You can even use HTML-like hyperlinks in the dialog (in
Text
,Footer,
andExpandedText
), if you only add thetfEnableHyperlinks
flag: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 aTNotifyEvent
. The URL of the link (the href of the a element, that is) is stored in theURL
public property of theTTaskDialog
:The Footer
You can use the
Footer
andFooterIcon
properties to create a footer. Theicon
property accepts the same values as theMainIcon
property.Using the
tfUseHiconFooter
flag and theCustomFooterIcon
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.You can make the checkbox initially checked by specifying the
tfVerificationFlagChecked
flag. Unfortunately, due to a bug (?) in the VCL implementation of theTTaskDialog
, the inclusion of this flag whenExecute
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 eachOnVerificationClicked
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):
这是旧内容,但为了完整性,我在此处添加此内容:
适用于 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+.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
这是我的简短文档:
不要使用它,除非您不希望您的应用程序在 XP 上运行。
像其他人一样,对 delphi doc wiki 内容提出改进建议,但是请注意短语“注意:任务对话框需要 Vista 或 Windows 7”。这是“不要使用它!”的代码。基本上,有人想到了完全支持新的 Windows Vista 对话框,其实现方式是编写仅调用对话框 API 的包装器代码。由于没有向您提供后备功能,因此您在 XP 上运气不好。
Here's my short documentation:
Don't use it, unless you don't want your application to work on XP.
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.