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:
The new Task Dialog API can give your application a fresh breath, but that comes with cost because it works only on Vista or better, with enabled themes. So, how to work around the problem if you need to run the application also in Windows XP or in machine without themes enabled? For button 6, there's a simple code to check whether you can safely use the TTaskDialog
component or whether you have to come back to normal ShowMessage
or MessageDlg
. Here's the event handler for the button 6:
Try to disable the themes for your application and click on button 6.
Obviously, it is strongly suggested that you wrap this code in a function so that you do not have to write the same check code repeatedly.
Tip
Downloading the example code
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
Log in or register to our website using your e-mail address and password.
Hover the mouse pointer on the SUPPORT tab at the top.
Click on Code Downloads & Errata.
Enter the name of the book in the Search box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
Click on Code Download.
You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Delphi-Cookbook-Second-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!