修改windowproc窗口钩子中lParam的值

Changing the value of lParam in WndProc windows hook

本文关键字:lParam 的值 windowproc 窗口 修改      更新时间:2023-10-16

我试图在我的c# WPF应用程序中更改矩形,其中窗口与shell挂钩最小化到HSHELL_GETMINRECT。

遵循这个Win32 C API重定向最小化动画我能够编组在lParam参数中返回的结构,但我不能改变它的值,以便windows获得新的矩形。

这是目前为止我的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;
}

我需要为Windows添加return new IntPtr(1);以使用新的结构。