如何定义' CreateDIB'在C#中运行或重现其目的?

时间:2016-05-21 23:26:29

标签: c# c++ .net vb.net winapi

我试图将此C ++代码从 this MSDN引用转换为C#或VB.NET:

    WITH tab AS
    (SELECT xmltype('
                 <bookstore>
        <book category="COOKING">
            <title lang="en">Cook Book</title>
            <author>Chef Author</author>
            <year>2015</year>
            <price>310.00</price>
        </book>
        <book category="CHILDREN">
            <title lang="en">Kid Story Book</title>
            <author>KJ Banter</author>
            <year>2010</year>
            <price>229.99</price>
        </book>
        </bookstore>
    ') col
    FROM dual
    )
    SELECT nodepath,nodevalue
      FROM tab t,
       xmltable('
        for $i in $tmp/descendant::*
        where $i/text() != ""
        return <R><P>{string-join($i/ancestor-or-self::*/name(), "/")}</P><V>{$i/text()}</V></R>' 
        passing t.col AS "tmp" columns
        nodepath varchar2(1000) path '//P',
        nodevalue varchar2(1000) path '//V')

乍一看似乎很简单,但我需要帮助才能了解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; 功能,我不知道该功能的用途和用途是什么,我无法找到相关信息关于,我也无法在Windows SDK头文件中找到它,什么都不是。

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

我找到了 CImageAllocator.CreateDIB ,我不确定它是否会引用它,但该函数的参数与CreateDIB的某种参数不对应就像我在 this 其他MSDN代码中看到的那样,所以概率不是同一个函数,而且它也是一个直接的东西......

嗯,这是我目前的翻译,它有效,但我担心可能的内存问题,因为缺少CreateDIB(width, height)函数或其等效的托管成员:

CreateDIB

1 个答案:

答案 0 :(得分:1)

用于设置缩略图图像的过多努力。只需在窗口上保留位图的副本,并在需要时绘制它。

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#因为问题上的标签就列出了它。