以编程方式触发 MouseLeftButtonDown 事件
我正在尝试以编程方式手动触发 WPF 控件上的 MouseLeftButtonDown 事件,因为我使用的是 Microsoft Surface SDK,它不会触发 MouseLeftButtonDown 事件,而是触发 ContactDown 事件。基本上,我试图将 MouseLeftButtonDown 事件下推到控件,以在处理 ContactDown 事件时触发控件上的正确行为。
我猜我必须以某种方式使用控件上的 RaiseEvent 方法来使用 MouseButtonEventArgs 来执行此操作,但我在计算参数时遇到了一些麻烦。
预先感谢您的帮助!
I'm trying to manually fire a MouseLeftButtonDown event on a WPF control programmatically, as I am using the Microsoft Surface SDK, which does not fire MouseLeftButtonDown events, but ContactDown events. Basically I'm trying to push the MouseLeftButtonDown event down to the control, to fire off the correct behavior on the control, while handling a ContactDown event.
I'm guessing I have to somehow use the RaiseEvent method on the control to do this with MouseButtonEventArgs, but I'm having some trouble figuring out the parameters.
Thanks in advance for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 Win32 互操作来欺骗鼠标和按键事件。研究 MSDN/pinvoke.net。
请注意,这将导致系统和其他应用程序认为鼠标实际上被单击了。如果您只想启动 WPF 事件,请尝试 RaiseEvent( new RoutedEventArgs( UIElement.MouseLeftButtonDownEvent ) ) 。
You can spoof mouse and key events using Win32 interop. Investigate the SendInput function on MSDN/pinvoke.net.
Note that this will cause the system and other applications to think the mouse was actually clicked. If you just want to initiate a WPF event, try
RaiseEvent( new RoutedEventArgs( UIElement.MouseLeftButtonDownEvent ) )
.希望触发控件中的某个事件通常表明代码中存在设计问题。事件处理程序应该触发行为,而不是执行行为。我建议您将执行由
MouseLeftButtonDown
事件处理程序触发的操作的代码移至单独的方法中。然后可以从ContactDown
事件处理程序调用相同的方法。The wish to trigger a certain event in a control is quite often an indicator of a design problem in the code. Event handlers should trigger behavior, not perform it. I would suggest that you move the code that performs the action triggered by the
MouseLeftButtonDown
event handler into a separate method. Then the same method can be called from theContactDown
event handler.这就是我在测试代码中触发事件的方式。
This is how I fire the event in my test code.
这是我用来引发任何
UIElement
的点击事件的静态方法。This is the static method I use to raise click events for any
UIElement
.