gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:25:46 26 4
gpt4 key购买 nike

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

正如上面引用链接所指出的,首先需要调用 DwmSetWindowAttribute 以启用 DWMWA_FORCE_ICONIC_REPRESENTATIONDWMWA_HAS_ICONIC_BITMAP 属性。

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

问题是,当我尝试为我的 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

最佳答案

根据 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 位图,它起作用了:

enter image description here

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

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

enter image description here

当悬停在上面时:

enter image description here

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


更新

DwmSetIconicThumbnail 函数返回错误的原因是图像超过了缩略图的最大尺寸,就是这样,Windows 本身不处理调整大小,所以我们需要做一点更多的工作。

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

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

这是一个工作代码示例:

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

关于c# - 如何从 C# 或 VB.Net 使用 Win32 'DwmSetIconicThumbnail'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37322340/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com