如何在C#或VB.Net中使用Win32的'DwmSetIconicThumbnail'?

4
我想使用 DwmSetIconicThumbnail 函数为我的应用程序的缩略图预览设置静态图像。如上述参考链接所指出,首先需要调用 DwmSetWindowAttribute 来启用 DWMWA_FORCE_ICONIC_REPRESENTATIONDWMWA_HAS_ICONIC_BITMAP 属性。我已经完成了所有这些步骤。我从WindowsAPICodePack源代码这里获取了所有定义,并遵循相同的步骤(或者我认为是这样)。问题在于,当我尝试将示例适应于我的 WinForms 窗口时,当我在以下代码的末尾调用 DwmSetIconicThumbnail 函数时,我会得到一个 E_INVALIDARG HRESULT 代码,我不确定有问题的参数是 hwnd 还是 hBitmap。我做错了什么?。

C#:

Bitmap bmp;
IntPtr hBitmap;
IntPtr hwnd;
int hresult;

const int DisplayThumbnailFrame = 0x1;
public enum DwmWindowAttribute : uint
{
    NcRenderingEnabled = 1,
    NcRenderingPolicy,
    TransitionsForceDisabled,
    AllowNcPaint,
    CaptionButtonBounds,
    NonClientRtlLayout,
    ForceIconicRepresentation,
    Flip3DPolicy,
    ExtendedFrameBounds,
    HasIconicBitmap,
    DisallowPeek,
    ExcludedFromPeek,
    Cloak,
    Cloaked,
    FreezeRepresentation,
    Last
}

[DllImport("dwmapi.dll", PreserveSig = true)]
static internal extern int DwmSetWindowAttribute(IntPtr hwnd, 
                                                 DwmWindowAttribute dwAttributeToSet, 
                                                 IntPtr pvAttributeValue, 
                                                 uint cbAttribute);

[DllImport("Dwmapi.dll")]
public static extern int DwmSetIconicThumbnail(IntPtr hwnd, 
                                               IntPtr hBitmap, 
                                               int flags);

private void Form1_Shown() {

    bmp = (Bitmap)Bitmap.FromFile("C:\\Image.jpg");
    hBitmap = bmp.GetHbitmap();
    hwnd = Process.GetCurrentProcess.MainWindowHandle;

    IntPtr block = Marshal.AllocHGlobal(4);
    int value = Math.Abs(Convert.ToInt32(true)); // or 1
    Marshal.WriteInt32(block, value);

    try {
        hresult = DwmSetWindowAttribute(hwnd, DwmWindowAttribute.HasIconicBitmap, block, 4);
        if ((hresult != 0)) {
            throw Marshal.GetExceptionForHR(hresult);
        }

        hresult = DwmSetWindowAttribute(hwnd, DwmWindowAttribute.ForceIconicRepresentation, block, 4);
        if ((hresult != 0)) {
            throw Marshal.GetExceptionForHR(hresult);
        }

    } finally {
        Marshal.FreeHGlobal(block);
    }

    hresult = DwmSetIconicThumbnail(hwnd, hBitmap, DisplayThumbnailFrame);
    if ((hresult != 0)) {
        throw Marshal.GetExceptionForHR(hresult);
    }

}

VB.NET:

Dim bmp As Bitmap
Dim hBitmap As IntPtr
Dim hwnd As IntPtr
Dim hresult As Integer

Const DisplayThumbnailFrame As Integer = &H1

Enum DwmWindowAttribute As UInteger
    NcRenderingEnabled = 1
    NcRenderingPolicy
    TransitionsForceDisabled
    AllowNcPaint
    CaptionButtonBounds
    NonClientRtlLayout
    ForceIconicRepresentation
    Flip3DPolicy
    ExtendedFrameBounds
    HasIconicBitmap
    DisallowPeek
    ExcludedFromPeek
    Cloak
    Cloaked
    FreezeRepresentation
    Last
End Enum

<DllImport("dwmapi.dll", PreserveSig:=True)>
Friend Shared Function DwmSetWindowAttribute(hwnd As IntPtr,
                                             dwAttributeToSet As DwmWindowAttribute,
                                             pvAttributeValue As IntPtr,
                                             cbAttribute As UInteger
) As Integer
End Function

<DllImport("Dwmapi.dll")>
Public Shared Function DwmSetIconicThumbnail(ByVal hwnd As IntPtr,
                                             ByVal hBitmap As IntPtr,
                                             ByVal flags As Integer
) As Integer
End Function

Private Sub Form1_Shown() Handles MyBase.Shown

    bmp = DirectCast(Bitmap.FromFile("C:\Image.jpg"), Bitmap)
    hBitmap = bmp.GetHbitmap()
    hwnd = Process.GetCurrentProcess.MainWindowHandle

    Dim block As IntPtr = Marshal.AllocHGlobal(4)
    Dim value As Integer = Math.Abs(CInt(True)) ' or 1
    Marshal.WriteInt32(block, value)

    Try
        hresult = DwmSetWindowAttribute(hwnd, DwmWindowAttribute.HasIconicBitmap, block, 4)
        If (hresult <> 0) Then
            Throw Marshal.GetExceptionForHR(hresult)
        End If

        hresult = DwmSetWindowAttribute(hwnd, DwmWindowAttribute.ForceIconicRepresentation, block, 4)
        If (hresult <> 0) Then
            Throw Marshal.GetExceptionForHR(hresult)
        End If

    Finally
        Marshal.FreeHGlobal(block)

    End Try

    hresult = DwmSetIconicThumbnail(hwnd, hBitmap, DisplayThumbnailFrame)
    If (hresult <> 0) Then
        Throw Marshal.GetExceptionForHR(hresult)
    End If

End Sub
3个回答

6
根据MSDN文档的说明:
应用程序通常在收到其窗口的WM_DWMSENDICONICTHUMBNAIL消息后调用DwmSetIconicThumbnail函数。缩略图不应超过该消息中指定的最大x坐标和y坐标。缩略图还必须具有32位色深度。
因此,使用以下32x32位图,具有32位色深度,它可以正常工作:

enter image description here

例外已经消失。但是,它并没有完全替换应用程序图标缩略图,而是添加了它。
这是使用ALT+TAB查看的样子:

enter image description here

当悬停在它上面时:

enter image description here

请注意,我没有修改您的代码,而是完全按照原样运行,只是使用了一个合适的位图。这些是在Windows 10机器上的结果。

更新

DwmSetIconicThumbnail 函数返回错误的原因是图像超过了缩略图的最大尺寸,这意味着 Windows 本身不会处理调整大小,因此我们需要做更多的工作。

我不确定哪个因素决定了我们可以为图像设定的最大缩略图大小,这只是猜测,但我认为它取决于一个 Windows 注册表值,该值确定缩略图预览的宽度和高度(我确切地不记得该值的注册表位置,抱歉,但可以轻松通过 Google 找到)。

好的,如上所述,我们需要处理 WM_DWMSENDICONICTHUMBNAIL (0x323) 消息,因此我们需要覆盖我们 Win32 窗口(表单)的基本窗口过程,即 WNDPROC,然后最终可以从消息参数中检索缩略图创建的最大宽度和高度。

以下是一个有效的代码示例:

Private Const WM_DWMSENDICONICTHUMBNAIL As Integer = &H323

Protected Overrides Sub WndProc(ByRef m As Windows.Forms.Message)

    Select Case m.Msg

        Case WM_DWMSENDICONICTHUMBNAIL

            Dim hwnd As IntPtr = Process.GetCurrentProcess().MainWindowHandle
            Dim dWord As Integer = m.LParam.ToInt32()
            Dim maxWidth As Short = BitConverter.ToInt16(BitConverter.GetBytes(dWord), 2)
            Dim maxHeight As Short = BitConverter.ToInt16(BitConverter.GetBytes(dWord), 0)

            Using img As Image = Bitmap.FromFile("C:\Image.jpg")

                Using thumb As Bitmap = CType(img.GetThumbnailImage(maxWidth, maxHeight, Nothing, Nothing), Bitmap)

                    Dim hBitmap As IntPtr = thumb.GetHbitmap()

                    Dim hresult As Integer = NativeMethods.DwmSetIconicThumbnail(hwnd, hBitmap, 0)
                    If (hresult <> 0) Then
                        ' Handle error...
                        ' Throw Marshal.GetExceptionForHR(hresult)
                    End If

                    NativeMethods.DeleteObject(hBitmap)

                End Using

            End Using

    End Select

    ' Return Message to base message handler.
    MyBase.WndProc(m)

End Sub

作为最后一条评论,如果将来我需要记住这个问题,我会分享我在MSDN上找到的这个问题,它对于遇到WM_DWMSENDICONICTHUMBNAIL消息问题的人可能有帮助:

答案指向了我错过的MSDN的最终结论,经过进一步的调查,我终于解决了它,感谢那个!但是这个答案只证明了32x32的小图标可以工作,这并不是一个真实情况下的解决方案。 - ElektroStudios
附加的图像出现的原因是因为设置了DISPLAYFRAME标志,但实际上应该是一个框架效果而不是"图像附加"。无论如何,这个效果对我没有影响,请不必担心。但你的回答提供的信息不够全面,我希望你能允许我在几分钟内补充所需的额外细节,我会付赏金奖励,再次感谢! - ElektroStudios

1

使用略微修改过的声明Microsoft Reference Source(提供返回值),我能够让它按预期运行。

internal static class NativeMethods
{
    [DllImport("dwmapi.dll")]
    public static extern int DwmSetIconicThumbnail(IntPtr hwnd, IntPtr hbmp, DWM_SIT dwSITFlags);

    [DllImport("dwmapi.dll")]
    public static extern int DwmSetWindowAttribute(IntPtr hwnd, DWMWA dwAttribute, ref int pvAttribute, int cbAttribute);

    public enum DWM_SIT
    {
        None,
        DISPLAYFRAME = 1
    }

    public enum DWMWA
    {
        NCRENDERING_ENABLED = 1,
        NCRENDERING_POLICY,
        TRANSITIONS_FORCEDISABLED,
        ALLOW_NCPAINT,
        CAPTION_BUTTON_BOUNDS,
        NONCLIENT_RTL_LAYOUT,
        FORCE_ICONIC_REPRESENTATION,
        FLIP3D_POLICY,
        EXTENDED_FRAME_BOUNDS,
        // New to Windows 7:
        HAS_ICONIC_BITMAP,
        DISALLOW_PEEK,
        EXCLUDED_FROM_PEEK
        // LAST
    }

    public const uint TRUE = 1;
}

然后,我只是修改了您现有的C#代码以适应这些签名。

    private void Form1_Shown(object sender, EventArgs e)
    {
        bmp = (Bitmap)Bitmap.FromFile("C:\\Image.jpg");
        hBitmap = bmp.GetHbitmap();
        hwnd = Process.GetCurrentProcess().MainWindowHandle;

        int attributeTrue = (int)NativeMethods.TRUE;
        hresult = NativeMethods.DwmSetWindowAttribute(hwnd, NativeMethods.DWMWA.HAS_ICONIC_BITMAP, ref attributeTrue, sizeof(int));
        if ((hresult != 0))
            throw Marshal.GetExceptionForHR(hresult);

        hresult = NativeMethods.DwmSetWindowAttribute(hwnd, NativeMethods.DWMWA.FORCE_ICONIC_REPRESENTATION, ref attributeTrue, sizeof(int));
        if ((hresult != 0))
            throw Marshal.GetExceptionForHR(hresult);

        hresult = NativeMethods.DwmSetIconicThumbnail(hwnd, hBitmap, NativeMethods.DWM_SIT.DISPLAYFRAME);
        if ((hresult != 0))
            throw Marshal.GetExceptionForHR(hresult);
    }

感谢你的评论,但是你可以在我的问题中看到,我也修改了 DwmSetWindowAttributeDwmSetIconicThumbnail 的声明以返回一个值...我们正在使用相同的方法。无论如何,我当然尝试验证你提供的解决方案是否可行,但是我在 DwmSetIconicThumbnail 函数中得到了相同的 HRESULT 代码(使用你的 Marshal 调用时出现 System.ArgumentException)。抱歉,它对我不起作用。 - ElektroStudios
供参考,我的关于retval的注释是为了表明我从MS的声明中做了哪些更改,而不是你的。但是由于我的示例对你来说无效,我怀疑@jstreet的答案可能比我的更相关。能否发布具体的图像? - cokeman19
再次感谢您的帮助,关于图像现在不相关,但是它的大小正如我所发现的那样。假设我使用了一个随机的大分辨率图像,它将失败,因为我期望Windows会将其调整大小以适应最大可能的缩略图大小,但我需要确定最大可能的大小,然后自己调整大小。问题已经解决!如果您有兴趣将此功能应用于您的应用程序,则在jstreet的答案中添加了一个代码示例!问候。 - ElektroStudios

0
也许硬编码的“4”大小起了作用? 看看(C# 取模)这是否有效:
...
IntPtr block = Marshal.AllocHGlobal(sizeof(int));
...
hresult = DwmSetWindowAttribute(hwnd, DwmWindowAttribute.HasIconicBitmap, block, sizeof(int));
...
hresult = DwmSetWindowAttribute(hwnd, DwmWindowAttribute.ForceIconicRepresentation, block, sizeof(int));

请注意,在调用 Bitmap.GetHbitmap() (https://msdn.microsoft.com/en-us/library/1dz311e4(v=vs.110).aspx#Anchor_2) 后,您可能需要释放位图使用的内存。

此外,这是一个关于封送的好入门文章:http://justlikeamagic.com/2010/03/09/marshaling/


这没有相关性,32位(Int)= 4字节。如果您注意到,我正在使用相同的标志(ForceIconicRepresentation,HasIconicBitmap),相同的数据类型(Int32)并传递相同的正确大小值(4),就像WindowsCodeApiPack的人一样。此外,该函数不返回任何失败的HRESULT代码,就像我提到的那个函数返回错误一样,但无论如何还是谢谢您的尝试。 - ElektroStudios
既然你正在抛出异常,那么异常详细信息提供了什么? - Frank Alvaro
没有详细信息,因为它不是托管异常,而是由DwmSetIconicThumbnail Win32函数返回的HRESULT代码,它引用了我解释过的 E_INVALIDARG (0x80070057),这相当于.NET中的 System.ArgumentException 异常:https://msdn.microsoft.com/en-us/library/9ztbc5s1%28v=vs.110%29.aspx - ElektroStudios

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接