在Taskdialog中使用隐藏的安全图标

时间:2014-02-09 07:02:49

标签: c# taskdialog

我正在尝试在TaskDialog消息框中显示安全成功图标(带蓝色背景)。这不是TaskDialogStandardIcon的枚举值之一。参考:http://dotnet.dzone.com/articles/using-new-taskdialog-winapi

如何将这些非标准值分配给((TaskDialog)发送方).Icon?它甚至可以在C#中使用吗? C#

任何指针都会非常有用。

此致 阿斯温

3 个答案:

答案 0 :(得分:4)

我认为您需要自己从comctl32.dll导入TaskDialog函数:

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

要从文件中使用您自己的图标,您需要导入TaskDialogIndirect

(顺便说一下,我为TaskDialogCommonIcon找到了许多其他有趣的图标样式。你可以添加例如:

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

答案 1 :(得分:3)

我知道这是一个古老的问题,但我正在寻找类似的东西,所以我想我会传递我所发现的东西。使用@KnorxThieus发布的信息,我找到了一种方法来使用"隐藏" TaskDialog中的安全图标,无需通过上面概述的DLLImport进程。使用他为TaskDialogCommonIcon枚举提供的实际值,我发现您可以简单地将它们转换为适当的类型(即TaskDialogCommonIcon),并且您的应用程序应该正确显示它们。

请注意,我使用的是Nuget(nuget.org/packages/WindowsAPICodePack-Core)的WindowsAPICodePack 1.1.2版,下面的代码已经使用Telerik Code Converter从{Visual Basic转换而来{{{ 3}}),因此您可能需要在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();
    }
}

在我的测试中,这似乎适用于列出的所有列举@KnorxThieus以及其他几个。我试图弄清楚是否有类似的方法将Icon属性设置为另一个(非标准)图像文件,但到目前为止我还没有成功。我希望这有助于将来偶然发现这一点的任何人。

答案 2 :(得分:0)

看看Ookii.Dialogs。它还实现了TaskDialog和其他对话框,并且具有针对WPFWindows Forms的版本。

enter image description here