欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# 在WebBrowser中用SendMessage模拟鼠标点击

程序员文章站 2024-03-08 11:24:52
复制代码 代码如下:using system; using system.collections.generic; using system.componentmodel;...
复制代码 代码如下:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
using system.runtime.interopservices;

namespace browsermouseclick
{
public partial class form1 : form
{
     [dllimport("user32.dll", charset = charset.auto, setlasterror = false)]
     static extern intptr sendmessage(intptr hwnd, uint msg, intptr wparam, intptr lparam);

     [dllimport("user32.dll", setlasterror = true)]
     static extern intptr getwindow(intptr hwnd, uint ucmd);

     [dllimport("user32.dll", charset = charset.auto)]
     static extern int getclassname(intptr hwnd, stringbuilder lpclassname, int nmaxcount);

     public form1()
     {
         initializecomponent();
     }

     private void form1_load(object sender, eventargs e)
     {
         webbrowser1.navigate("http://www.devpub.com");
     }

     private void btnmouseclick_click(object sender, eventargs e)
     {
         int x = 100; // x coordinate of the click
         int y = 80; // y coordinate of the click
         intptr handle = webbrowser1.handle;
         stringbuilder classname = new stringbuilder(100);
         while (classname.tostring() != "internet explorer_server") // the class control for the browser
         {
             handle = getwindow(handle, 5); // get a handle to the child window
             getclassname(handle, classname, classname.capacity);
         }

         intptr lparam = (intptr)((y << 16) | x); // the coordinates
         intptr wparam = intptr.zero; // additional parameters for the click (e.g. ctrl)
         const uint downcode = 0x201; // left click down code
         const uint upcode = 0x202; // left click up code
         sendmessage(handle, downcode, wparam, lparam); // mouse button down
         sendmessage(handle, upcode, wparam, lparam); // mouse button up
     }
}
}

想在webbrowser控件里面模拟鼠标点击,在百度上找了半天,怎么也找不到,还是google强大,在一个国外网站上找到的,代码比较清楚了,不做说明。