DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. Using the new TaskDialog via WinAPI

Using the new TaskDialog via WinAPI

Denzel D. user avatar by
Denzel D.
·
Apr. 28, 10 · Interview
Like (0)
Save
Tweet
Share
11.76K Views

Join the DZone community and get the full member experience.

Join For Free

With Windows Vista (and now with Windows 7) a replacement has been introduced for the standard MessageBox. It is called the TaskDialog. It is an advanced version of the MessageBox control , adding more customization and message types. If you used Windows Vista/7 for a while, you’ve probably seen the security popups as well as messages generated by the latest native Windows applications – those look nothing like the old style messages.

In this article I am going to focus on a more basic version of the custom task dialog (without custom options), however it should give you a solid understanding of what the control can do and how to use it instead of the regular MessageBox.

There are two ways a developer can go to display a TaskDialog – use a wrapper or call WinAPI directly. I am that kind of developer that tries to avoid wrappers unless necessary, simply to avoid yet another dependency layer and eliminate a possible source of bugs (I am not implying that wrappers will necessarily carry bugs, but there is such a possibility). Therefore, in this article I am going to show how to call the TaskDialog directly.

First of all, the DLL signature should be inserted in the code:

[DllImport("comctl32.dll", CharSet = CharSet.Unicode,  EntryPoint="TaskDialog")]
static extern int TaskDialog(IntPtr hWndParent, IntPtr hInstance, String pszWindowTitle,
String pszMainInstruction, String pszContent, int dwCommonButtons, IntPtr pszIcon, out int pnButton);

Here I am creating a simple TaskDialog that will display a message, without an icon and with the OK button as the only option:

int result;
TaskDialog(this.Handle, IntPtr.Zero, "Sample TaskDialog", "This is the header", "Contents here", 1, new IntPtr(0), out result);

The sixth parameter represents the set of buttons to be shown in the dialog. The button indexes are shown below:

OK – 1
Cancel – 8
Yes – 2
No – 4
Retry – 10
Close – 20

These indexes can be used together. For example, if I want to put both OK and Cancel, I will write the statement like this:

TaskDialog(this.Handle, IntPtr.Zero, "Sample TaskDialog", "This is the header", "Contents here", 1 | 8, new IntPtr(0), out result);

Note that you can only have one instance of a button of a specific type in a TaskDialog. Therefore, repeating an index won’t add multiple buttons of the same types.

The clicked button will be returned in the result variable.

The sample dialog will look like this:

This is quite simple. Let’s get some icons on it, that will be displayed depending of the type of the message. At this moment, there are ten types you can use. I am going to show each one of them, with their index as well as with a screenshot of how it looks like below.

Question (0)

Information (UInt16.MaxValue -2)

Warning (UInt16.MaxValue)

Stop (UInt16.MaxValue -1)

Security Error (UItn16.MaxValue – 6)
 

Security Warning (UInt16.MaxValue – 5)

Security Success (UInt16.MaxValue – 7)
 

Security Shield (UInt16.MaxValue - 3)
 

Security Shield (Blue) (UInt16.MaxValue – 4)
 

Security Shield (Gray) (UInt16.MaxValue – 8)
 

The index is passed as IntPtr (seventh parameter) with the help of a statement like new IntPtr ((int)value). Therefore, I can have a statement like this to display an error:

TaskDialog(this.Handle, IntPtr.Zero, "Sample TaskDialog", "This is the header", "Contents here", 1 | 8, new IntPtr((int)UInt16.MaxValue - 1), out result);

This pretty much covers the TaskDialog and its basic usage. You can detect the pressed button by the returned result, so you can wire it to an event handler.
The reason why I was not using enums in this case to bundle all possible dialog styles and buttons is because I am not creating a wrapper. For example, if I am only going to use the TaskDialog once in the application (that is less likely to happen but still possible), then I don’t need the additional code baggage for unused types. If you are sure that you will be using multiple styles as well as button combinations, it would be a good idea to create a separate class with the above mentioned enums.

security Dialog (software) application Icon dev Moment Dependency Task (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Continuous Development: Building the Thing Right, to Build the Right Thing
  • 7 Awesome Libraries for Java Unit and Integration Testing
  • Tech Layoffs [Comic]
  • Agile Transformation With ChatGPT or McBoston?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: