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?
If you can't find the documentation, then write it:
The Hello World of a Task Dialog
Captionis the text shown in the titlebar of the window,Titleis the header, andTextis the body matter of the dialog. Needless to say,Executedisplays the task dialog, and the result is shown below. (We will return to theCommonButtonsproperty 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
CommonButtonsproperty 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
TModalResultvalue will be stored in theModalResultproperty as soon asExecutehas returned. TheMainIconproperty 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,MainIconcan 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
DefaultButtonproperty 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
CommonButtonsproperty 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
tfUseCommandLinksflag (inFlags). Now you can also set theCommandLinkHint(per-button) property:The
tfAllowDialogCancellationflag 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
ExpandedTextandExpandedButtonCaptionto 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
tfExpandFooterAreaflag, 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
tfExpandedByDefaultflag.Custom Icons
You can use any custom icon in a task dialog, by using the
tfUseHiconMainflag and specifying theTIconto use in theCustomMainIconproperty.Hyperlinks
You can even use HTML-like hyperlinks in the dialog (in
Text,Footer,andExpandedText), if you only add thetfEnableHyperlinksflag: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
OnHyperlinkClickedevent, which is aTNotifyEvent. The URL of the link (the href of the a element, that is) is stored in theURLpublic property of theTTaskDialog:The Footer
You can use the
FooterandFooterIconproperties to create a footer. Theiconproperty accepts the same values as theMainIconproperty.Using the
tfUseHiconFooterflag and theCustomFooterIconproperty, 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
VerificationTextstring 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
tfVerificationFlagCheckedflag. Unfortunately, due to a bug (?) in the VCL implementation of theTTaskDialog, the inclusion of this flag whenExecutehas 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 eachOnVerificationClickedevent, 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):