如何在 C# 中定义'CreateDIB'函数或重现其用途?

How to define 'CreateDIB' function or reproduce its purpose in C#?

本文关键字:函数 定义 CreateDIB      更新时间:2023-10-16

我正试图将此C++代码从MSDN引用翻译为C#或VB.NET:

case WM_DWMSENDICONICTHUMBNAIL:
{    
// This window is being asked to provide its iconic bitmap. This indicates
// a thumbnail is being drawn.
hbm = CreateDIB(HIWORD(lParam), LOWORD(lParam)); 
if (hbm)
{
hr = DwmSetIconicThumbnail(hwnd, hbm, 0);
DeleteObject(hbm);
}
}
break;

乍一看似乎很简单,但我需要帮助来了解CreateDIB函数,我不知道该函数的含义和用途,我找不到有关的信息,而且我在Windows SDK头文件中也找不到它,什么都没有。

该函数在哪里定义?,有必要遵循C++示例中的良好实践?,如何从C#中声明它,或者哪个是。NET等效的非托管函数?。

我找到了CImageAllocator。CreateDIB,我不确定它是否引用了它,但该函数的参数与我在这个其他MSDN代码中看到的CreateDIB(width, height)不对应,所以可能不是同一个函数,而且它是一个直接显示的东西。。。

好吧,这是我目前所做的翻译,它是有效的,但我担心可能的内存问题,因为缺乏CreateDIB函数或其等效的托管成员:

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

设置缩略图花费了太多精力。只需在您的窗口上保留位图的副本,并在需要时绘制即可。

public partial class Form1 : Form
{
[DllImport("Dwmapi.dll")]
static extern int DwmSetIconicThumbnail(IntPtr hWnd, IntPtr hbmp, uint dwSITFlags);
[DllImport("Dwmapi.dll")]
static extern int DwmSetWindowAttribute(IntPtr hWnd, uint dwAttribute, IntPtr pvAttribute, uint cbAttribute);
const uint WM_DWMSENDICONICTHUMBNAIL = 0x0323;
const uint DWMWA_FORCE_ICONIC_REPRESENTATION = 7;
const uint DWMWA_HAS_ICONIC_BITMAP = 10;
Size thumbSize = new Size(30, 30);
Bitmap thumbImage = new Bitmap(30, 30);
object sync = new object();
public Form1()
{
InitializeComponent();
using (Graphics g = Graphics.FromImage(this.thumbImage))
{
g.Clear(Color.Blue);
g.DrawRectangle(Pens.Black, new Rectangle(new Point(0, 0), this.thumbSize));
}
this.HandleCreated += Form1_HandleCreated;
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_DWMSENDICONICTHUMBNAIL)
{
lock (this.sync)
{
int x = (int)((m.LParam.ToInt32() >> 16) & 0xffff);
int y = (int)(m.LParam.ToInt32() & 0xffff);
if (this.thumbSize != new Size(x, y))
{
this.thumbSize = new Size(x, y);
this.UpdateBitmap();
}
DwmSetIconicThumbnail(this.Handle, thumbImage.GetHbitmap(), 0);
}
}
base.WndProc(ref m);
}
void UpdateBitmap()
{
lock (this.sync)
{
this.thumbImage = new Bitmap(this.thumbSize.Width, this.thumbSize.Height);
using (Graphics g = Graphics.FromImage(this.thumbImage))
{
g.Clear(Color.Blue);
g.DrawRectangle(Pens.Black, new Rectangle(new Point(0, 0), this.thumbSize));
//or: g.DrawImage() with stretching specified.
}
}
}
private void Form1_HandleCreated(object sender, EventArgs e)
{
IntPtr val = Marshal.AllocHGlobal(4);
Marshal.WriteInt32(val, 1);
DwmSetWindowAttribute(this.Handle, DWMWA_FORCE_ICONIC_REPRESENTATION, val, 4);
DwmSetWindowAttribute(this.Handle, DWMWA_HAS_ICONIC_BITMAP, val, 4);
Marshal.FreeHGlobal(val);
}
}

C#由于问题列表上的标签它。