top of page

TaskDialog ... MsgBox plus

  • Writer: John
    John
  • 3 days ago
  • 3 min read
This post is one of a series providing implementation examples of Windows API Functions, Types, Enums and Consts using VBA. The code in this post can be used as-is, however, if you regularly (or even just occasionally) work with Windows API declarations in VBA, you may want to see the posts Automatically add Windows API declaration(s) and Using 'F1' to view Windows API web pages which explain some of the functionality that can be added to the VBE by VBE_Extras.

This is the second of four posts relating to TaskDialog and TaskDialogIndirect. This post provides an example of how to use the TaskDialog Windows API Function along with some associated Types, Enums and Consts. To make the code in this post work, you will also need the code from the first of the four posts, TaskDialog and TaskDialogIndirect helper functionality.


TaskDialog provides "MsgBox-like" functionality - but enhanced with options for both a 'main instruction' heading and 'content' within the body of the dialog and options for many more buttons. Here are some examples along with example code to generate these dialogs (using the CTaskDialog Class Module which is provided below) ...


A 'basic' example


A basic TaskDialog

A 'many buttons' example


TaskDialog with many buttons

An example with a 'documented' icon


That is, the fact that the icon is available is included in the Windows API documentation.

TaskDialog with a 'documented' icon

An example with an 'undocumented' icon


That is, the fact that the icon is available is not included in the Windows API documentation ... but it is still a standard icon (i.e. it is identified using a Const value not, for example, loaded from a file).

TaskDialog with an 'undocumented' icon

So, MsgBox-like ... but with a few extra options:

  • Both 'main instruction' and 'content' in the body of the dialog ... note that you do not have to have both of these, both are optional (but the dialog looks a bit odd with neither of them!)

  • Many more buttons ... but only with specific texts

  • More choices for icons (some with 'banners' as in the 'undocumented' example above) ... but you can only choose from a pre-defined set of standard icons


The Class Module code


So this is the code that provides a 'wrapper' for the TaskDialog Windows API Function and actually generates the dialog ... you should add this to your Project as a Class Module named CTaskDialog ...



The code is documented to explain what is going on. Some things worth noting are:


  • It includes a TODO that you will want to review ... you can locate TODO comments in code using the Tasks command of VBE_Extras - see Sometimes it's just the little things - #4: Tasks (aka TODOs) for details.

  • TaskDialog (and TaskDialogIndirect) require Common Controls version 6. If the code is running in a host application that is using Common Control version 5 then the Show() Function includes code to create and activate (and subsequently deactivate and release) an 'activation context' which temporarily loads a Common Controls version 6 manifest. See the TaskDialog and TaskDialogIndirect helper functionality post for further details on this.

  • The code here is intended to be an example of using the specific Windows API Functions / Types / Enums and Consts … it is not intended to be production-ready code … you may want to extend and re-work the code to be production ready, for example: the Show() Function actually returns a Boolean to indicate whether the dialog was successfully shown; this code has no error handling; the TODO (mentioned above) should be reviewed.



Comments


bottom of page