Question

I am trying to display a Security Success Icon (with the blue background) in TaskDialog message box. This is not one of the enum values of TaskDialogStandardIcon. Reference: http://dotnet.dzone.com/articles/using-new-taskdialog-winapi.

How do I assign these non standard values to ((TaskDialog)sender).Icon ? Is it even possible in C#? C#

Any pointers would be really helpful.

Regards, Ashwin

Was it helpful?

Solution

I think you will need to import TaskDialog function from comctl32.dll yourself:

static class TaskDialogWrapper
{
    [DllImport("comctl32.dll", CharSet = CharSet.Unicode, EntryPoint = "TaskDialog")]
    static extern int TaskDialog(IntPtr hWnd, IntPtr hInstance, string pszWindowTitle, string pszMainInstruction, string pszContent, TaskDialogCommonButton dwCommonButtons, IntPtr pszIcon, out IntPtr pnButton);

    public static TaskDialogCommonButton Show(IntPtr handle, IntPtr instance, string title, string instructionText, string content, TaskDialogCommonButton commonButtons, TaskDialogCommonIcon commonIcon)
    {
        IntPtr resultButton;
        if (TaskDialog(handle, instance, title, instructionText, content, commonButtons, new IntPtr((int)commonIcon), out resultButton) != 0)
            throw new InvalidOperationException();
        return (TaskDialogCommonButton)resultButton;
    }
}

[Flags()]
enum TaskDialogCommonButton
{
    Ok = 0x1,
    Yes = 0x2,
    No = 0x4,
    Cancel = 0x8,
    Retry = 0x10,
    Close = 0x20
}

enum TaskDialogCommonIcon
{
    ShieldGrey = 65527,
    ShieldOk = 65528,
    ShieldError = 65529,
    ShieldWarning = 65530,
    ShieldBlue = 65531,
    Shield = 65532,
    Information = 65533,
    Error = 65534,
    Warning = 65535,
}

To use your own icon from a file, you will need to import TaskDialogIndirect.


(Btw., I found many other interesting icon styles for TaskDialogCommonIcon. You could add e.g.:

enum TaskDialogCommonIcon
{
    None = 0,
    Sheet = 2,
    ExplorerFolderOpen = 3,
    ExplorerFolderFlat = 5,
    ExplorerFolderLeft = 6,
    Search = 8,
    ExplorerFolderClosed = 10,
    ExplorerGames = 14,
    Application = 15,
    TransparentSpace = 17,
    ExplorerSearch = 18,
    TextFile = 19,
    Letter = 20,
    Picture = 21,
    Diashow = 103,
    // ...
}

OTHER TIPS

I know that this is an old question, but I was looking for something similar, so I thought I'd pass along what I've found. Using the information posted by @KnorxThieus, I found a way to use the "hidden" security icons in the TaskDialog without going through the DLLImport process outlined above. Using the actual values he provided for the TaskDialogCommonIcon enumeration, I found that you can simply cast them to the appropriate type (i.e., the TaskDialogCommonIcon), and your application should display them properly.

Please note, I'm using the WindowsAPICodePack version 1.1.2 from Nuget (nuget.org/packages/WindowsAPICodePack-Core), and the code below has been converted from Visual Basic using the Telerik Code Converter (http://converter.telerik.com/), so it's possible that you may have to do some fine-tuning in C#:

if (TaskDialog.IsPlatformSupported) {
    using (TaskDialog dialog = new TaskDialog()) {
        dialog.Caption = "TESTING";
        dialog.InstructionText = "THIS IS A TEST";
        dialog.Text = "This is a test of casting a value to the desired Icon type for a TaskDialog.";

        // Produces the green shield with green background
        dialog.Icon = (TaskDialogStandardIcon)65528;
        dialog.OwnerWindowHandle = this.Handle;
        dialog.Show();
    }
}

In my testing, this seems to work for all of the enumerations @KnorxThieus listed, as well as several others. I'm trying to figure out if there's a similar method for setting the Icon property to another (non-standard) image file, but I've been unsuccessful with that so far. I hope this helps anyone that stumbles across this in the future.

Take a look at the Ookii.Dialogs. It implements the TaskDialog and others dialogs as well, and have versions targeting WPF and Windows Forms.

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top