【问题标题】:Performing a mouse click without moving cursor在不移动光标的情况下执行鼠标单击
【发布时间】:2011-09-06 05:12:08
【问题描述】:

除了通过Cursor 类移动光标,点击mouse_event 然后将光标移动到其旧位置之外,我找不到任何解决方案。我现在正在使用SendInput 功能,但仍然没有机会找到一个好的解决方案。有什么建议吗?

【问题讨论】:

  • .Net C#、VB、ASP.Net 是什么味道
  • 我编辑了标签。谢谢提醒。
  • 你想点击什么类型的对象?
  • 我正在尝试单击浏览器上的某个位置。首先,我得到我想要点击的点的坐标,然后我运行点击函数。

标签: c# .net mouseevent mouse


【解决方案1】:

您应该使用 Win32 API。 使用来自 user32.dll 的 pInvoked SendMessage

pInvoked Function

然后阅读鼠标事件: Mouse Input on msdn

然后阅读:System events and Mouse Mess.......

还有很多信息: Info

【讨论】:

    【解决方案2】:

    这是一个遵循 Hooch 建议的方法的示例。

    我创建了一个包含 2 个按钮的表单。当您单击第一个按钮时,第二个按钮的位置就确定了(屏幕坐标)。然后检索此按钮的句柄。最后,SendMessage(...) (PInvoke) 函数用于在不移动鼠标的情况下发送点击事件。

    public partial class Form1 : Form
    {
        [DllImport("user32.dll")]
        private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, 
            IntPtr wParam, IntPtr lParam);
    
        [DllImport("user32.dll", EntryPoint = "WindowFromPoint", 
            CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr WindowFromPoint(Point point);
    
        private const int BM_CLICK = 0x00F5;
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            // Specify the point you want to click
            var screenPoint = this.PointToScreen(new Point(button2.Left, 
                button2.Top));
            // Get a handle
            var handle = WindowFromPoint(screenPoint);
            // Send the click message
            if (handle != IntPtr.Zero)
            {
                SendMessage( handle, BM_CLICK, IntPtr.Zero, IntPtr.Zero);
            }
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hi", "There");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      • 2021-03-26
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多