c# 在WebBrowser中用SendMessage模擬鼠標點擊
更新時間:2010年02月06日 14:04:10 作者:
想在WebBrowser控件里面模擬鼠標點擊,在百度上找了半天,怎么也找不到,還是google強大,在一個國外網(wǎng)站上找到的,代碼比較清楚了,不做說明。
復(fù)制代碼 代碼如下:
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強大,在一個國外網(wǎng)站上找到的,代碼比較清楚了,不做說明。
相關(guān)文章
asp.net下實現(xiàn)URL重寫技術(shù)的代碼
asp.net下實現(xiàn)URL重寫技術(shù)的代碼...2007-10-10ASP.NET項目開發(fā)中日期控件DatePicker如何使用
這篇文章主要為大家詳細介紹了ASP.NET項目開發(fā)中日期控件DatePicker的使用方法,感興趣的小伙伴們可以參考一下2016-04-04ASP.NET?MVC使用Log4Net記錄異常日志并跳轉(zhuǎn)到靜態(tài)頁
這篇文章介紹了ASP.NET?MVC使用Log4Net記錄異常日志并跳轉(zhuǎn)到靜態(tài)頁的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09ASP.NET實現(xiàn)根據(jù)URL生成網(wǎng)頁縮略圖的方法
這篇文章主要介紹了ASP.NET實現(xiàn)根據(jù)URL生成網(wǎng)頁縮略圖的方法,結(jié)合實例較為詳細的分析了asp.net生成網(wǎng)頁縮略圖的詳細實現(xiàn)技巧與相關(guān)注意事項,需要的朋友可以參考下2015-11-11基于asp.net下使用jquery實現(xiàn)ajax的解決方法
本文以最簡單的方法為新手示范如何使用jquery實現(xiàn)ajax技術(shù)(所以本文是專為新手所寫,老鳥勿噴,大神此處省略一萬字)。至于什么是jquery什么是ajax,自己谷歌去2013-05-05