3

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

1
  • You may want to check out the Ookii.Dialogs, which implements the TaskDialog and others Commented Oct 24, 2018 at 3:48

3 Answers 3

5

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,
    // ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

I know this is a bit older, but where did you find these specific enumerations (in your "BTW" section)? I'd like to see if I can find a complete listing of them somewhere without having to manually identify each one. I figured out that I can use the WindowsAPICodePack from Nuget (nuget.org/packages/WindowsAPICodePack-Core) to call these by setting the Icon property to a casted value (CType(65528, TaskDialogCommonIcon)), and I'd like to try to get as complete a listing as possible.
I'm afraid to disappoint you, but the way I found out these constants has been trying out them all. Nevertheless, there might exist any DLL storing the same icons in the same order (e. g. imageres? I don't know.) you could google to find out the icon's official meaning.
Ancient post but yes, it appears that you can use the same int values as the icon IDs in imageres.dll. You can see the IDs using something like Nirsoft's IconsExtract. I'm not sure where the special ones like with the colored background bar are from though.
3

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.