从低级鼠标钩C#返回的错误鼠标坐标 [英] Erroneous Mouse Coordinates Returned from Low Level Mouse Hook C#

查看:116
本文介绍了从低级鼠标钩C#返回的错误鼠标坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建WPF应用程序,其中涉及一些将Form元素嵌入到同一渲染层的黑客行为.本质上,我要做的是在屏幕外渲染表单,并在WPF图像中对表单进行模拟.我还有一个低级的鼠标钩,如果将它们发往Form实体,则会从WPF应用程序中窃取事件,而是使用PostMessage(...)将事件发送到隐藏的form元素.

I'm building a WPF application with some hackery involved to embed Form elements on the same render layer. Essentially what I do is I render the form off-screen and have a mock-up of the form in a WPF image. I also have a low level mouse hook which steals events from the WPF application if they are destined for the Form mock-up and instead use PostMessage(...) to send the events off to the hidden form element.

如果我从钩子过程中返回一个非零值来表示要吃掉该事件(即使我仍然调用队列中的所有鼠标钩子),则光标也会停留在一个位置.我假设这是因为在事件未到达的某种WPF应用程序层中处理了光标位置.

If I return a non-zero value from my hook procedure indicating to eat the event (even if I still call all the mouse hooks in the queue), the cursor gets stuck in one position. I'm assuming this is because the cursor position gets handled in some sort of WPF application layer that the event isn't reaching.

我认为完全可以阻止WPF应用程序知道该事件,因为我可以自己设置光标位置-毕竟,鼠标事件附带有坐标.不幸的是,这些鼠标坐标似乎非常不正确.实际上,无论我的光标位于何处,我总是会收到相同的坐标.

I figured that it was fine to prevent the WPF application from knowing about the event at all because I could just set the cursor position myself-- there are coordinates attached to a mouse event after all. Unfortunately, it seems that these mouse coordinates are horribly incorrect. In fact, no matter where my cursor is located, I always receive the same coordinates.

这是我的代码:

if (nCode >= 0){
    MOUSEHOOKSTRUCT_LL mousehookstruct_ll1 =
                    ((MOUSEHOOKSTRUCT_LL)Marshal.PtrToStructure(((IntPtr)lParam), typeof(MOUSEHOOKSTRUCT_LL)));
    if (mousehookstruct_ll1 != null) {
        if ((user != null) && user.SystemMouseHookProc(nCode, wParam, lParam, new Point(mousehookstruct_ll1.pt_x, mousehookstruct_ll1.pt_y), mousehookstruct_ll1.dwExtraInfo)) {
            return new IntPtr(1);// CallNextHookEx(this.MessageHookHandle, 1, wParam, lParam);// It doesn't matter that I don't call CallNextHook here.
         }
     }
 }
 GC.KeepAlive(this);
 return CallNextHookEx(this.MessageHookHandle, nCode, wParam, lParam);

}

然后在user.SystemMouseHookProc(...)中打印出正确的光标位置,然后打印出鼠标钩所拉出的坐标,并且输出始终如下所示:

Then in user.SystemMouseHookProc(...) I print out the correct cursor position followed by the coordinates pulled by the mouse hook, and the output is always something like the following:

光标:523,578

Cursor: 523,578

{X = 1985777551,Y = 1985777602}//此坐标永不变

{X=1985777551,Y=1985777602} //This coordinate never changes

该输出显然是错误的.我该怎么做才能从鼠标钩中获取正确的鼠标坐标?

That output is clearly wrong. What can I do to get the correct mouse coordinates from a mouse hook?

谢谢.

P.S.该解决方案源自一种流行的在线解决方案.不幸的是,该解决方案无法满足我的需求,因此我不得不将其更改为这种形式.

P.S. This solution is derived from a popular one online. Unfortunately, that solution didn't meet my needs so I've had to alter it to this form.

推荐答案

由于任何原因,静态属性System.Windows.Forms.Control.MousePosition无法正常工作?

Any reason the static property System.Windows.Forms.Control.MousePosition won't work?

这篇关于从低级鼠标钩C#返回的错误鼠标坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆