5

我是使用 WPF 的新手。我有一个带有数据网格的 WPF 窗口,当双击发生时,它会启动一个进程。这很好用,但是当我在平板电脑(使用 Windows 7)中使用触摸屏执行此操作时,该过程永远不会发生。所以我需要用触摸事件来模拟双击事件。任何人都可以帮助我做到这一点,好吗?

4

2 回答 2

0

请参阅如何在 C# 中模拟鼠标单击?关于如何模拟鼠标单击(在 Windows 窗体中),但它通过以下方式在 WPF 中工作:

using System.Runtime.InteropServices;

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

    private const int MOUSEEVENTF_LEFTDOWN = 0x02;
    private const int MOUSEEVENTF_LEFTUP = 0x04;
    private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
    private const int MOUSEEVENTF_RIGHTUP = 0x10;

    public void DoMouseClick()
    {
         //Call the imported function with the cursor's current position
        int X = //however you get the touch coordinates;
        int Y = //however you get the touch coordinates;
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
    }
}
}
于 2012-07-11T23:06:12.253 回答
0

首先添加一个Mouse事件点击函数:

/// <summary>
/// Returns mouse click.
/// </summary>
/// <returns>mouseeEvent</returns>
public static MouseButtonEventArgs MouseClickEvent()
{
    MouseDevice md = InputManager.Current.PrimaryMouseDevice;
    MouseButtonEventArgs mouseEvent = new MouseButtonEventArgs(md, 0, MouseButton.Left);
    return mouseEvent;
}

将单击事件添加到您的 WPF 控件之一:

private void btnDoSomeThing_Click(object sender, RoutedEventArgs e)
{
    // Do something
}

最后,从任意函数调用 click 事件:

btnDoSomeThing_Click(new object(), MouseClickEvent());

要模拟双击,请添加一个双击事件,如 PreviewMouseDoubleClick 并确保任何代码都在单独的函数中开始:

private void lvFiles_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    DoMouseDoubleClick(e);
}

private void DoMouseDoubleClick(RoutedEventArgs e)
{
    // Add your logic here
}

要调用双击事件,只需从另一个函数(如 KeyDown)调用它:

private void someControl_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key == Key.Enter)
        DoMouseDoubleClick(e);
}
于 2014-04-24T14:05:37.287 回答