Any reason the static property System.Windows.Forms.Control. MousePosition won't work?
Up vote -1 down vote favorite share g+ share fb share tw.
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.
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. 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. Here is my code: 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); } 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: Cursor: 523,578 {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? Thank you. 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. C# wpf mouseevent winforms-interop mouse-hook link|improve this question asked Nov 3 '11 at 16:03user10280401.
Just as a sidenote, you have to add a reference to System.Windows. Forms since this is a WPF application. – Brian Deragon Nov 3 '11 at 16:20 It seems I was marshalling things to the wrong struct.
Apparently MOUSEHOOKSTRUCT_LL is incorrect, but the following is correct... StructLayout(LayoutKind. Sequential) private struct MSLLHOOKSTRUCT { public POINT pt; public uint mouseData; public uint flags; public uint time; public IntPtr dwExtraInfo; } – user1028040 Nov 3 '11 at 16:26 Yeah, System.Windows.Forms.Control. MousePosition won't work because I'm at such a low level that I was intercepting and eating the mouse move event before the cursor was allowed to move.
Though it won't let me answer my own question yet, it turns out I was just using a similarly named, but incorrect struct to retrieve data. – user1028040 Nov 3 '11 at 16:29 Got ya, no problem! I just try to go for the simple built-in solutions first, before hitting other frameworks or raw win32.
– Brian Deragon Nov 3 '11 at 16:31.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.