以编程方式触发 MouseLeftButtonDown 事件

发布于 2024-09-05 03:09:40 字数 306 浏览 10 评论 0原文

我正在尝试以编程方式手动触发 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼泪也成诗 2024-09-12 03:09:40

您可以使用 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 ) ).

蘑菇王子 2024-09-12 03:09:40

希望触发控件中的某个事件通常表明代码中存在设计问题。事件处理程序应该触发行为,而不是执行行为。我建议您将执行由 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 the ContactDown event handler.

帅的被狗咬 2024-09-12 03:09:40
var grid = new Grid();            

int timestamp = new TimeSpan(DateTime.Now.Ticks).Milliseconds;
const MouseButton mouseButton = MouseButton.Left;
var mouseDownEvent =
   new MouseButtonEventArgs(Mouse.PrimaryDevice, timestamp, mouseButton) {
       RoutedEvent = UIElement.MouseLeftButtonDownEvent,
       Source = grid,
   };

这就是我在测试代码中触发事件的方式。

var grid = new Grid();            

int timestamp = new TimeSpan(DateTime.Now.Ticks).Milliseconds;
const MouseButton mouseButton = MouseButton.Left;
var mouseDownEvent =
   new MouseButtonEventArgs(Mouse.PrimaryDevice, timestamp, mouseButton) {
       RoutedEvent = UIElement.MouseLeftButtonDownEvent,
       Source = grid,
   };

This is how I fire the event in my test code.

仅冇旳回忆 2024-09-12 03:09:40

这是我用来引发任何 UIElement 的点击事件的静态方法。

using System;
using System.Windows;
using System.Windows.Input;

    
public static void RaiseMouseClickEvent(UIElement elementToClick, MouseButton buttonToUse)
{
    elementToClick.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, Environment.TickCount, buttonToUse) { RoutedEvent = Button.ClickEvent });
}

This is the static method I use to raise click events for any UIElement.

using System;
using System.Windows;
using System.Windows.Input;

    
public static void RaiseMouseClickEvent(UIElement elementToClick, MouseButton buttonToUse)
{
    elementToClick.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, Environment.TickCount, buttonToUse) { RoutedEvent = Button.ClickEvent });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文