WndProc挂钩lParam到xy跳线

本文关键字:xy 跳线 lParam 挂钩 WndProc | 更新日期: 2023-09-27 18:28:17

在使用WndProc挂钩从Win32 API获取消息后,我正在尝试获取鼠标引线。。

下面是我的代码。。它不长,应该很容易理解。。我在学习这一切,只是不知道如何将lParam更改为点x和y。

任何帮助都会很好,谢谢:)

    private const int WM_LEFTBUTTONDOWN = 0x0201;
    private const int WM_LEFTBUTTONUP = 0x0202;
    private const int WM_MOUSEMOVE = 0x0200;
    private const int WM_MOUSEWHEEL = 0x020A;
    private const int WM_RIGHTBUTTONDOWN = 0x0204;
    private const int WM_RIGHTBUTTONUP = 0x0205;

    public MainWindow()
    {
        InitializeComponent();
    }
    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
        source.AddHook(WndProc);
    }
    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        if (msg == WM_MOUSEMOVE)
        {
            label1.Content = "Msg: " + msg + " wParam: " + wParam + " lParam: " + lParam;
        }
        return IntPtr.Zero;
    }

WndProc挂钩lParam到xy跳线

您可以使用Point(int dw)构造函数:

Point point = new Point(lParam.ToInt32());
...

来自MSDN关于int dw参数:

dw参数的低位16位指定新点的水平x坐标,高位16位指定垂直y坐标。

x坐标在低位16位,y坐标在后面的16位。像这样破解:

int x = (short)lParam.ToInt32();
int y = lParam.ToInt32() >> 16;

winuser.h 中使用MSLLHOOKSTRUCT结构

请参阅:winuser/ns winuser msllhookstruct

private struct POINT
{
    public int x;
    public int y;
}
private struct MSLLHOOKSTRUCT
{
    public POINT pt;
    public uint mouseData, flags, time;
    public IntPtr dwExtraInfo;
}
MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
int x = hookStruct.pt.x;
int y = hookStruct.pt.y;