Search code examples
c#taskdialog

Using Hidden Security Icons in Taskdialog


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


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,
        // ...
    }