如何从 C# 或 VB.Net 使用 Win32 'DwmSetIconicThumbnail'?

How to use Win32 'DwmSetIconicThumbnail' from C# or VB.Net?

我想使用 DwmSetIconicThumbnail 功能为我的应用程序的缩略图预览设置静态图像。

正如上面参考文献link中指出的,首先需要调用DwmSetWindowAttribute来启用DWMWA_FORCE_ICONIC_REPRESENTATIONDWMWA_HAS_ICONIC_BITMAP属性。

我已经做到了。我已经从 WindowsAPICodePack 源代码 here 中获取了所有定义,并且我正在执行相同的步骤(或者我是这么认为的)。

问题是,当我尝试为我的 WinForms Window 调整示例时,我在调用下面代码末尾的 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

也许尺寸的硬编码“4”在起作用? 看看(c# mods here)是否有效:

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

根据MSDN Documentation

An application typically calls the DwmSetIconicThumbnail function after it receives a WM_DWMSENDICONICTHUMBNAIL message for its window. The thumbnail should not exceed the maximum x-coordinate and y-coordinate that are specified in that message. The thumbnail must also have a 32-bit color depth.

因此,使用以下具有 32 位颜色深度的 32×32 位图,它起作用了:

异常消失了。但是,它并没有完全取代应用程序图标缩略图,而是附加了它。

这是使用 ALT+TAB 时的样子:

当鼠标悬停在上面时:

请注意,我根本没有修改您的代码,运行 完全按照原样,只是使用了合适的 Bitmap。这些是 Windows 10 台机器的结果。


更新

DwmSetIconicThumbnail 函数 returns 出错的原因是图像超出了缩略图的最大尺寸,仅此而已,Windows 本身不处理调整大小,所以我们需要做更多的工作。

我不确定哪个因素决定了我们可以为图像建立的最大可能缩略图大小,这是推测,但我认为这取决于决定宽度和高度的 Windows 注册表值用于缩略图预览(我完全不记得该值的注册表位置,抱歉,但可以很容易地用 Google 搜索)。

好吧,如上所述,我们需要处理 WM_DWMSENDICONICTHUMBNAIL (0x323) 消息,因此我们需要覆盖基础 Window 过程 a.k.a WNDPROC of our Win32 window (a Form), 最后我们可以从消息参数中获取创建缩略图的最大宽度和高度。

这是一个工作代码示例:

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 消息问题的人可能会有帮助:

使用来自 Microsoft Reference Source 的稍微修改的声明(以提供 return 值),我能够使其按预期运行。

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);
    }