2012-07-11 19 views
5

Sono nuovo utilizzando WPF. Ho una finestra WPF con un datagrid che avvia un processo quando si verifica un doppio clic. Questo funziona benissimo, ma quando lo faccio in un tablet (con Windows 7), utilizzando il touch screen, il processo non avviene mai. Quindi ho bisogno di emulare l'evento doppio clic con eventi touch. Qualcuno può aiutarmi a fare questo, per favore?Emulare evento doppio clic in Datagrid con touchDown

risposta

0

Vedi How to simulate Mouse Click in C#? per come emulare un clic del mouse (in Windows-forme), ma funziona in WPF facendo:

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); 
    } 
} 
} 
+0

grazie mille! – sonseiya

+0

@somseiya Nessun problema –

0

Prima aggiungere una funzione del mouse evento click:

/// <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; 
} 

Aggiungi un evento click per uno dei controlli WPF:

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

Infine, Chiama l'evento click da qualsiasi funzione:

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

Per simulare un doppio clic, aggiungere un doppio evento click come PreviewMouseDoubleClick e assicurarsi che qualsiasi codice inizia in una funzione separata:

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

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

Per invocare il doppio evento click, basta chiamare da un altro funzione (come TastoGiù):

private void someControl_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) 
{ 
    if (e.Key == Key.Enter) 
     DoMouseDoubleClick(e); 
}