鼠标模拟点击webBrowser中元素的坐标

using System;

 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Text;
 7 using System.Windows.Forms;
 8 using System.Runtime.InteropServices;
 9 
10 namespace BrowserMouseClick
11 {
12     public partial class Form1 : Form
13     {
14         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
15         static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
16 
17         [DllImport("user32.dll", SetLastError = true)]
18         static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
19 
20         [DllImport("user32.dll", CharSet = CharSet.Auto)]
21         static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
22 
23         public Form1()
24         {
25             InitializeComponent();
26         }
27 
28         private void Form1_Load(object sender, EventArgs e)
29         {
30             webBrowser1.Navigate("http://www.devpub.com");
31         }
32 
33         private void btnMouseClick_Click(object sender, EventArgs e)
34         {
35             int x = 100; // X coordinate of the click 
36             int y = 80; // Y coordinate of the click 
37             IntPtr handle = webBrowser1.Handle;
38             StringBuilder className = new StringBuilder(100);
39             while (className.ToString() != "Internet Explorer_Server") // The class control for the browser 
40             {
41                 handle = GetWindow(handle, 5); // Get a handle to the child window 
42                 GetClassName(handle, className, className.Capacity);
43             }
44 
45             IntPtr lParam = (IntPtr)((y << 16) | x); // The coordinates 
46             IntPtr wParam = IntPtr.Zero; // Additional parameters for the click (e.g. Ctrl) 
47             const uint downCode = 0x201; // Left click down code 
48             const uint upCode = 0x202; // Left click up code 
49             SendMessage(handle, downCode, wParam, lParam); // Mouse button down 
50             SendMessage(handle, upCode, wParam, lParam); // Mouse button up 
51         }
52     }
53 }
原文地址:https://www.cnblogs.com/yeye518/p/2854215.html