i am new using WPF. I have a WPF Window with a datagrid wich launch a process when double click occurs. This work great, but when i do this in a tablet(with windows 7), using the touch screen, the process never happen. So i need to emulate the double click event with touch events. Any one can help me to do this, please?
Asked
Active
Viewed 2,197 times
2 Answers
0
See How to simulate Mouse Click in C#? for how to emulate a mouse click (in windows-forms), but it works in WPF by doing:
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);
}
}
}

Community
- 1
- 1

Liam McInroy
- 4,339
- 5
- 32
- 53
0
First add a Mouse event click function:
/// <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;
}
Add a click event to one of your WPF controls:
private void btnDoSomeThing_Click(object sender, RoutedEventArgs e)
{
// Do something
}
Finally, Call the click event from any function:
btnDoSomeThing_Click(new object(), MouseClickEvent());
To simulate double clicking, add a double click event like PreviewMouseDoubleClick and make sure any code starts in a separate function:
private void lvFiles_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DoMouseDoubleClick(e);
}
private void DoMouseDoubleClick(RoutedEventArgs e)
{
// Add your logic here
}
To invoke the double click event, just call it from another function (like KeyDown):
private void someControl_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
DoMouseDoubleClick(e);
}

Marcel Pennock
- 27
- 4