WndProc Hook lParam到xy绳索?

Chr*_*zio 2 c# wpf winapi

我试图在使用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;
    }
Run Code Online (Sandbox Code Playgroud)

dac*_*cap 6

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

Point point = new Point(lParam.ToInt32());
...
Run Code Online (Sandbox Code Playgroud)

从MSDN关于int dw参数:

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


Han*_*ant 5

x 坐标位于低 16 位,y 坐标位于接下来的 16 位。像这样破解它:

int x = (short)lParam.ToInt32();
int y = lParam.ToInt32() >> 16;
Run Code Online (Sandbox Code Playgroud)