c# 在WebBrowser中用SendMessage模拟鼠标点击
更新时间:2010年02月06日 14:04:10 作者:
想在WebBrowser控件里面模拟鼠标点击,在百度上找了半天,怎么也找不到,还是google强大,在一个国外网站上找到的,代码比较清楚了,不做说明。
复制代码 代码如下:
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强大,在一个国外网站上找到的,代码比较清楚了,不做说明。
相关文章
asp.net 删除项目文件/文件夹IIS重启,Session丢失问题
最近在做一个项目,涉及到大量文件中转(先上传到本项目的某个文件夹中,在移动到FTP中),后面发现每次一删除文件之后在做操作都会提示未登录,刚开始以为是WebService Session丢失问题,后面发现缓存也更新了2011-12-12
ASP.NET实现TreeView的XML数据源绑定实例代码
这篇文章介绍了ASP.NET实现TreeView的XML数据源绑定实例代码,有需要的朋友可以参考一下2013-11-11
实例解析Java中的synchronized关键字与线程安全问题
首先要清楚的是synchronized锁住的不是代码而是对象,因而在编写相关的代码块时要注意线程同步安全问题,下面就来以实例解析Java中的synchronized关键字与线程安全问题2016-06-06
Entity Framework加载控制Loading Entities
本文详细讲解了Entity Framework加载控制Loading Entities的用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-03-03
asp.net Reporting Service在Web Application中的应用
由于我们这个项目中使用微软的报表服务(Reporting Services)作为报表输出工具,本人也对它进行一点点研究,虽没有入木三分,但这点知识至少可以在大部分Reporting Service的场景中应用。2008-11-11


最新评论