2015-10-04 45 views
0

我試圖將窗口最小化的矩形改爲使用shell鉤子到我的C#WPF應用程序中的HSHELL_GETMINRECT。在WndProc窗口掛鉤中更改lParam的值

此後Win32 C API for redirecting minimize animation 我能夠編組在lParam參數中返回的結構,但我無法更改它的值,以便窗口獲取新的矩形。

這是我迄今爲止的WndProc方法。

public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) 
{ 
    // If windows sends the HSHELL_GETMINRECT event, a window in the taskbar is minimizing or maximizing. 
    if (wParam.ToInt32() == WindowInterop.HSHELL_GETMINRECT) 
    { 
     var param = Marshal.PtrToStructure<WindowInterop.MinRectParam>(lParam); 
     var icon = FindIcon(param.hWnd); 
     var rect = new WindowInterop.SRect 
     { 
      Bottom = (short)(icon.Y + icon.Height), 
      Left = (short)icon.X, 
      Right = (short)(icon.X + icon.Width), 
      Top = (short)icon.Y 
     }; 
     var newParam = new WindowInterop.MinRectParam 
     { 
      hWnd = param.hWnd, 
      Rect = rect 
     }; 
     // As I understand it, this will only create a new IntPtr pointing to the structure, 
     // it won't write the new structure to the existing pointer's location. 
     Marshal.StructureToPtr(newParam, lParam, true); 
     handled = true; 
    } 
    return IntPtr.Zero; 
} 
+1

實際上,'Marshal.StructureToPtr'會複製到指針指向的位置,所以它不會創建一個新的指針。你不應該轉向這種方法。嘗試將其更改爲false。這是必要的,但你可能會遇到更多問題。 –

+0

剛剛嘗試過,它沒有馬上工作,但當我也添加'''返回新的IntPtr(1);'''它工作!謝謝。 –

回答

0

我需要添加return new IntPtr(1);爲Windows使用新的結構。