【问题标题】:Emulate double click event in Datagrid with touchDown使用 touchDown 在 Datagrid 中模拟双击事件
【发布时间】:2012-07-11 16:02:05
【问题描述】:

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

【问题讨论】:

    标签: c# wpf datagridview mouse emulation


    【解决方案1】:

    请参阅How to simulate Mouse Click in 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);
        }
    }
    }
    

    【讨论】:

      【解决方案2】:

      先添加一个鼠标事件点击函数:

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

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

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

      【讨论】:

        猜你喜欢
        • 2013-08-31
        • 2017-01-15
        • 2019-02-09
        • 2014-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多