TTaskdialog
is a dialog box somewhat like the standard call to Application.MessageBox
in the VCL but much more powerful. Task Dialog API has been available since Windows Vista and Windows Server 2008, and your application must be theme enabled to use it (go to Project | Options | Application | Runtime Themes | Enable Runtime Themes).
Besides the usual default set of buttons (OK, Cancel, Yes, No, Retry, and Close), you can define extra buttons and many other customizations. The following Windows API provides task dialogs:
More information about API utilization can be obtained from https://msdn.microsoft.com/en-us/library/windows/desktop/bb787471(v=vs.85).aspx.
While the API can be useful in some border cases, the VCL comes with a very nice component that does all the low-level stuff for us. Let's see the sample program that shows how it is simple to create a modern look and feel application.
Open the TaskDialogs.dproj
project and understand how it works.
There are six buttons on the form. The first one shows a simple utilization of the Task Dialog API, while the other five show a different utilization of the TTaskDialog
component, which wraps that API.
The first button uses the Windows API directly with the following code:
The TaskDialog
function is declared inside the Winapi.CommCtrl.pas
unit. So far, you could ask, "Why should I use a component for TaskDialogs
? Seems quite simple." Yes, it is, if you only want to mimic MessageDlg
, but things get complicated very fast if you want to use all the features of the Task Dialog API. So, the second button uses the TTaskDialog
component. Let's see the relevant properties configured at design time for the tdSimple
component:
Note
You can check the runtime appearance also at design time by double-clicking on the component over your form, or by selecting Test Dialog from the menu over the component. You can access the menu by right-clicking on the component.
As you can see, only the minimum properties have been set, just to show the power of the component. This configuration shows up a dialog with two buttons labelled Yes and No. The TTaskDialog
component can be configured at design time using the Object Inspector, or can be configured at runtime by code. In this first example, the configuration is defined at design time so that at runtime we only have to call the Execute
method and read the user response. Here's the code that actually uses the tdSimple
instance:
Even in this case, it is quite simple, but let's go deeper with the configuration. Let's say that we need a TaskDialog similar to the following screenshot:
Using the plain API is not so simple to do this. So, let's see how to configure the component:
The preceding block of code contains the definition for the three radio buttons. The following code shows the dialog and the retrieval of the result:
Even in this case, we have defined the properties at design time so that the runtime code is quite simple. Just note that the user choice is stored in the RadioButton.ID
property.
The TTaskDialog.Flags
property can greatly change the behavior of the dialog. Here's the meaning of each element of its set:
The real power of TaskDialogs comes when you build your dialog at runtime. Let's check what the fourth button does under the hood:
It seems like a lot of code, but it is simple and can be easily parameterized and reused inside your program. The resultant dialog is as shown:
The third choice allows the user to search on Google about the program executable name. This is not a common choice in the MessageDlg
dialog where buttons are predefined, but using the Task Dialog you can even ask something "strange" to the user (such as "do you want to ask Google about it?")
To achieve a better apparent speed, progress bars are great! The Task Dialog API provides a simple way to use progress bars inside dialogs. The classic Delphi solution relays a custom form with a progress bar and some labels (just like the "Compiling" dialog that you see when you compile a program within the Delphi IDE). However, in some cases, you need some simple stuff done and a Task Dialog is enough. If TTaskDialog
has the tfCallbackTimer
flag and tfShowProgressBar
, the OnTimer
event will be called every 200 milliseconds (five times a second), and the dialog will show a progress dialog that you can update within the OnTimer
event handler. However, the OnTimer
event handler runs in the main thread so that all the related advice applies (if the UI becomes unresponsive, consider a proper background thread and a queue to send information to the main thread).
This is the design time configuration of TTaskDialog tdProgress
:
There are two event handlers, one to handle click on the Cancel
button inside the dialog and one to handle the callback:
To not block the main
thread, the prime numbers are calculated a few at a time. When the calculation is ended, the callback is disabled by setting the OnTimer
event handler to nil
.
In other words, the real calculation is done in the main
thread, so you should slice your process in to smaller parts so that it can be executed one (small) piece at time.
The following code fires the progress Task Dialog:
Here's the resultant dialog: