3

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

如何将这些非标准值分配给 ((TaskDialog)sender).Icon ?在 C# 中甚至可能吗?C#

任何指针都会非常有帮助。

问候, 阿什温

4

3 回答 3

5

我认为您需要TaskDialog从自己导入功能comctl32.dll

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,
    // ...
}
于 2016-09-01T06:54:58.830 回答
3

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

请注意,我使用的是 Nuget (nuget.org/packages/WindowsAPICodePack-Core) 的 WindowsAPICodePack 1.1.2 版,下面的代码已使用 Telerik 代码转换器 ( http://converter.telerik ) 从 Visual Basic 转换而来.com/ ),因此您可能需要在 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属性设置为另一个(非标准)图像文件,但到目前为止我一直没有成功。我希望这对将来偶然发现此问题的任何人有所帮助。

于 2016-11-28T23:03:58.587 回答
0

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

在此处输入图像描述

于 2018-10-24T03:52:06.153 回答