我想使用 DwmSetIconicThumbnail 函数为我的应用程序的缩略图预览设置静态图像。如上述参考链接所指出,首先需要调用 DwmSetWindowAttribute 来启用
DWMWA_FORCE_ICONIC_REPRESENTATION
和 DWMWA_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