我是WPF的新手。我有一个带有数据网格的WPF窗口,它会在双击时启动进程。这项工作很棒,但是当我在平板电脑(使用Windows 7)中使用触摸屏执行此操作时,过程永远不会发生。所以我需要使用触摸事件模拟双击事件。有人可以帮我这么做吗?
答案 0 :(得分:0)
有关如何模拟鼠标单击(在Windows窗体中),请参阅How to simulate Mouse Click in C#?,但它可以在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);
}
}
}
答案 1 :(得分:0)
首先添加鼠标事件点击功能:
/// <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控件添加click事件:
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);
}