2016-05-21 22 views
-1

我想這款C++翻譯從this MSDN參考代碼,C#或VB.NET:如何定義'CreateDIB'函數或在C#中重現其目的?

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; 

乍一看似乎很簡單,但我需要幫助瞭解有關​​3210功能,我不知道該功能的用途和目的,我無法找到有關信息,也無法在Windows SDK頭文件中找到它,什麼也沒有。

在哪裏定義函數?,需要遵循該C++示例中的良好實踐?,如何從​​C#聲明它,或者哪個.NET是該非託管函數的等價物?

我發現CImageAllocator.CreateDIB這我不知道它是否reffers到,但函數的參數並不對應於一種CreateDIB(width, height)就像我在this其他MSDN代碼所示,所以probablly是不一樣的功能,也是一個directshow的事情...

嗯,這是目前的翻譯我做了,它的工作原理,但我擔心可能的內存問題,因爲缺少​​3210函數或其等價物託管會員:

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 
+1

'CreateDIB'不是Win32函數;我的猜測是這是一個函數,它調用'CreateDIBSection',然後將縮略圖渲染到位圖中。 –

+1

您鏈接到的MSDN文章表示這是來自較大示例項目的片段。但是我無法下載完整的項目,它似乎不再可用。所以我猜CreateDIB只是他們在這個示例中寫的一個函數。 – Saeed

+0

https://github.com/pauldotknopf/WindowsSDK7-Samples/blob/e8fe83b043727e71f5179da11fc6228475e7973c/multimedia/directshow/baseclasses/winutil.cpp#L1632? – andlabs

回答

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#因爲問題上的標籤列表它。

相關問題