Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I raise a mouse event in WPF / C# with specific coordinates?

Tags:

c#

mouseevent

wpf

I'd like to raise a mouse event (a click, mousedown, or mouseup) by taking a user's click anywhere in a WPF window and translating it by a known difference, e.g. click at x,y, raise the click event at x+100, y+100.

The underlying problem is that there's a display monitor that physically moves relative to an overlaying touch screen. Rather than recalibrating the touchscreen with every move, I'd like to add the translation offset to the click event.

I've looked at the Win32 API for mouse_event and its superseding function, SendInput. I admit I'm lost as I'm not very familiar with the API.

Surely this is a simple problem to solve, but I can't find example code anywhere that gets me to where I can implement a solution. Any help, pointers, or solid examples of how to add this to my code behind would be appreciated.

Thanks Mark

like image 698
Mark Eichman Avatar asked Jun 13 '11 01:06

Mark Eichman


1 Answers

Win32 API won't work with WPF, this link might help

 // Copied the snippet of code from the link above, just to help future readers
 MouseEventArgs e = new MouseEventArgs(Mouse.PrimaryDevice, 0);
 e.RoutedEvent = Mouse.MouseEnterEvent;

 youUIElement.RaiseEvent(e);
 // Or
 InputManager.Current.ProcessInput(e);
like image 192
Waleed Avatar answered Nov 17 '22 14:11

Waleed