如何在WPF/C#中使用特定坐标引发鼠标事件?

Mar*_*man 7 c# wpf mouseevent

我想通过在WPF窗口中的任意位置进行用户单击并按已知差异进行翻译来引发鼠标事件(单击,mousedown或mouseup),例如单击x,y,在x +处引发单击事件100,y + 100.

根本问题在于显示监视器相对于覆盖的触摸屏物理移动.我不想在每次移动时重新校准触摸屏,而是想将翻译偏移量添加到click事件中.

我查看了用于mouse_event的Win32 API及其取代函数SendInput.我承认我迷失了,因为我对API不是很熟悉.

当然这是一个需要解决的简单问题,但是我无法在任何地方找到能够实现解决方案的示例代码.任何有关如何将此添加到我的代码后面的帮助,指针或实例都将不胜感激.

谢谢马克

Wal*_*eed 8

Win32 API不适用于WPF,此链接可能有所帮助

 // 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);
Run Code Online (Sandbox Code Playgroud)

  • 好吧,你应该使用Event Bubbling来捕获在子元素上引发的每个特定事件.一旦捕获,您将获得引发事件的UIElement的引用,然后您可以指定新坐标并转发它们.要检测坐标是否在客户区之外,只需尝试:if(this.Clip.Bounds.Bottom> newY || this.Clip.Bounds.Left> newX dont)这里指的是你的主窗口 (2认同)