模拟鼠标在浏览器中单击不移动光标

问题描述:

我创建了一个窗体并在窗体中添加了两个按钮和一个浏览器。我用一个程序来获取鼠标坐标并点击登录按钮。我想在不移动光标的情况下实现这一点。 我不想查找登录按钮ID或使用HTML请求/响应。模拟鼠标在浏览器中单击不移动光标

任何想法是否可以在浏览器中单击非HTML按钮而不移动光标。

之下,但仅适用于表单中的其他控件,但不是因为这是形式的一部分

using System; 
using System.Drawing; 
using System.Runtime.InteropServices; 
using System.Windows.Forms; 

namespace simulateclicktest 
{ 
    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 = new Point(157 
       ,455); 
      // 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("clicked"); 
     } 

    } 
} 

尝试找到按钮的ID,并使用此代码浏览器的工作原理是代码:

private void button1_Click(object sender, EventArgs e) 
{ 
    webBrowser1.Document.GetElementById("button_ID").InvokeMember("click"); 
} 

如果您在窗体上拥有web浏览器控件并知道按钮的ID,则不需要DllImport。

+0

正如我所说的,我很想用鼠标单击按钮而不移动光标。 – user3726459

+0

对。此代码模拟网页浏览器中的按钮点击。您可以在任何功能中使用(不仅仅是按钮点击事件)。 – c4pricorn

+0

我已经知道如何做到这一点,这不完全是我正在寻找的。我正在查找代码以将鼠标单击消息发送给应用程序。我有我的代码工作。 SendMessage(hWnd,0x0201,0,myparam);而不是SendMessage(句柄,BM_CLICK,IntPtr.Zero,IntPtr.Zero);登录按钮不是HTML – user3726459