using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Diagnostics;namespace ClickButton_SendMessage { public delegate bool CallBack(IntPtr hwnd, int lParam); public partial class frmMain : Form { [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern IntPtr FindWindow(string strClass, string strWindow); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern IntPtr EnumChildWindows(IntPtr hWndParent, CallBack lpEnumFunc, int lParam); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern int GetWindowRect(IntPtr hwnd,RECT lpRect); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern int SetCursorPos(int x,int y); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern int SetForegroundWindow(IntPtr hwnd); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern void mouse_event(int dwFlags,int dx,int dy,int cButtons, int dwExtraInfo); public const int MOUSEEVENTF_LEFTDOWN = 0x2; public const int MOUSEEVENTF_LEFTUP = 0x4; [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern int GetWindowText(IntPtr hwnd, string lpString, int cch); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern int GetWindowTextLength(IntPtr hwnd); public struct RECT { public int Left; public int Top; public int Right; public int Bottom; } public struct POINT { public int x; public int y; } public static int nCount; public frmMain() { InitializeComponent();
} public static bool EnumWindowsProc(IntPtr h, int lParam) { string strText=new string((char)0,255); RECT hRect=new RECT(); nCount = nCount + 1;
btn = doc.GetElementById("login");
if (btn != null)
{
btn.InvokeMember("click");
}
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
下面的代码假设你已经建立了一个Windows Form,上面有一个WebBrowser名为“webBrowser”。Study Case 1:用WinForm的Event Handler响应Web页面的事件现在有这样一个Windows Application,它的界面上只有一个WebBrowser,显示一个本地的HTML文件作为界面。现在的问题是,所有逻辑都可以放在HTML文件里,唯独“关闭”按钮遇到了困难——通常,Web页面是没有办法直接控制浏览器的,更不用说结束这个WinForm程序了。但是,在.Net 2.0当中,“由Windows Form响应Web页面的事件”已经成为了现实。在.Net 2.0中,整个HTML文档以及其包含的各个HTML元素,都和一个个HtmlDocument、HtmlElement之类的.Net对象对应。因此只要找到这个“关闭”按钮对应的HtmlElement对象,为其click事件添加Event Handler即可。
假设HTML源代码如下:
<html>
<body>
<input type="button" id="btnClose" value="关闭" />
</body>
</html>那么找出该按钮并为之添加Event Handler的代码如下:HtmlDocument htmlDoc = webBrowser.Document;
HtmlElement btnElement = htmlDoc.All["btnClose"];
if (btnElement != null)
{
btnElement.click += new HtmlElementEventHandler(HtmlBtnClose_Click);
}其中 HtmlBtnClose_Click是按下Web按钮时的Event Handler。很简单吧?那么稍稍高级一点的——我们都知道一个HTML元素可能有很多各种各样的事件,而HtmlElement这个类只给出最常用、共通的几个。那么,如何响应其他事件呢?这也很简单,只需要调用HtmlElement的AttachEventHandler就可以了:btnElement.AttachEventHandler("onclick", new EventHandler(HtmlBtnClose_Click));
//这一句等价于上面的 btnElement.click += new HtmlElementEventHandler(HtmlBtnClose_Click);对于其他事件,把"onclick"换成该事件的名字就可以了。例如:
formElement.AttachEventHandler("onsubmit", new EventHandler(HtmlForm_Submit));Study Case 2:表单(form)的自动填写和提交
要使我们的WebBrowser具有自动填表、甚至自动提交的功能,并不困难。假设有一个最简单的登录页面,输入用户名密码,点“登录”按钮即可登录。已知用户名输入框的id(或Name,下同)是username,密码输入框的id是password,“登录”按钮的id是submitbutton,那么我们只需要在webBrowser的 DocumentCompleted事件中使用下面的代码即可:HtmlElement btnSubmit = webBrowser.Document.All["submitbutton"];
HtmlElement tbUserid = webBrowser.Document.All["username"];
HtmlElement tbPasswd = webBrowser.Document.All["password"];
if (tbUserid == null || tbPasswd == null || btnSubmit == null)
return;
tbUserid.SetAttribute("value", "smalldust");
tbPasswd.SetAttribute("value", "12345678");
btnSubmit.InvokeMember("click");这里我们用SetAttribute来设置文本框的“value”属性,用InvokeMember来调用了按钮的“click”方法。因为不同的Html元素,其拥有的属性和方法也不尽相同,所以.Net 2.0提供了统一的HtmlElement来概括各种Html元素的同时,提供了这两个方法以调用元素特有的功能。关于各种Html元素的属性和方法一览,可以查阅MSDN的DHTML Reference。※关于表单的提交,的确还有另一种方法就是获取form元素而不是button,并用form元素的 submit方法:HtmlElement formLogin = webBrowser.Document.Forms["loginForm"];
//……
formLogin.InvokeMember("submit");本文之所以没有推荐这种方法,是因为现在的网页,很多都在submit按钮上添加onclick事件,以对提交的内容做最基本的验证。如果直接使用form的submit方法,这些验证代码就得不到执行,有可能会引起错误。
1首先要获得这个button所在窗体的句柄(API 函数获得) 然后API SendMessage 函数去执行点击这个操作(其中的点击参数 楼主查查)
2 点击后 停顿sleep 几秒 为了弹出这个是否登录成功的验证框 3然后根据提示框的title去找是否存在这个窗体的句柄(API findwindown 这个函数 具体的楼主去查查) 如果单位时间内没出现 则判读成功
(合适的方法是检验是否出现了登录成功后出现的窗体句柄) 否则则过个几秒 继续点击执行先前的操作
SendMessage(Button1.Handle, WM_LBUTTONDOWN,0,0); 如果不知道按钮句柄
则FindWindow()获取窗体句柄
SetCursorPos 设置鼠标位置
mouse_event(MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
使用Windows函数
::PostMessage(m_hTarget, WM_MOUSEMOVE, MK_LBUTTON, MAKELPARAM(pt2.x, pt2.y));
来发送鼠标点击, 点击之后,Sleep(500),然后取按钮的颜色,判断是不是和已经按下的颜色相同。
如果不是,则按钮没有按下,继续点击,再取颜色,反之,则不再点击。看看这篇blog 里面有鼠标模拟操作游戏的 原理和代码
http://blog.csdn.net/jianuMan/archive/2010/06/17/5676194.aspx
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;namespace ClickButton_SendMessage
{
public delegate bool CallBack(IntPtr hwnd, int lParam);
public partial class frmMain : Form
{ [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr FindWindow(string strClass, string strWindow); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr EnumChildWindows(IntPtr hWndParent, CallBack lpEnumFunc, int lParam); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowRect(IntPtr hwnd,RECT lpRect); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int SetCursorPos(int x,int y); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int SetForegroundWindow(IntPtr hwnd); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern void mouse_event(int dwFlags,int dx,int dy,int cButtons, int dwExtraInfo);
public const int MOUSEEVENTF_LEFTDOWN = 0x2;
public const int MOUSEEVENTF_LEFTUP = 0x4; [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hwnd, string lpString, int cch); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowTextLength(IntPtr hwnd);
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
} public struct POINT
{
public int x;
public int y;
} public static int nCount; public frmMain()
{
InitializeComponent();
}
public static bool EnumWindowsProc(IntPtr h, int lParam)
{ string strText=new string((char)0,255);
RECT hRect=new RECT(); nCount = nCount + 1;
int Ret=0;
Ret = GetWindowTextLength(h);
string sSave = new string((char)9, Ret);
GetWindowText(h, sSave, Ret + 1);
if(sSave.IndexOf("Click Me",0) > -1)//"Click Me"是按钮的名称 按你的就是“登录”
{
//GetWindowRect(h, hRect);
//SetCursorPos((hRect.Left + hRect.Right) / 2, (hRect.Top + hRect.Bottom) / 2);
SetCursorPos(200, 160); //这里是直接通过Spy++获取的按钮“Click Me”的位置
SetForegroundWindow(h);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
return false;
}
return true;
} private void button1_Click(object sender, EventArgs e)
{
IntPtr h;
string strName = "Demo"; //新建一个项目 主窗体标题是"Demo"。这里可直接换成你的登录框的标题
h = FindWindow(null, strName);
Debug.Print(h.ToString());
if (h.ToInt32() != 0)
{
CallBack myCallBack = new CallBack(frmMain.EnumWindowsProc);
EnumChildWindows(h, myCallBack, 0);
}
}
}
}
//如果想不停的点击 加个Timer不停点击。 或者加个变量IsLogin = FindWindow(null,"登陆成功后的标题 或 登录失败提示标题")
只有登录按钮所在的窗体在激活时才能点中。 可以用ShowWindow()先激活窗体,然后再点击操作
Mouse_event api
1、通过API函数,找到窗口的句柄
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);2、找到按钮的句柄3、通过下面的API发送消息
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
我曾经想干坏事
可惜没用 别用高级语言把 C#绝对没戏 C++还有点用
顺便 梦幻真WINIO那个都封了
BS你
DND都没说什么
注意 此DND非那个横板PK的那个恶心的DND
调用api Declare Sub mouse_event()Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const MOUSEEVENTF_LEFTDOWN = &H2 '模拟鼠标左键按下
Public Const MOUSEEVENTF_LEFTUP = &H4 ’模拟鼠标左键释放
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 '模拟鼠标中间键按下
Public Const MOUSEEVENTF_MIDDLEUP = &H40 '模拟鼠标中间键释放
Public Const MOUSEEVENTF_RIGHTDOWN = &H8 '模拟鼠标右键按下
Public Const MOUSEEVENTF_RIGHTUP = &H10 '模拟鼠标右键释放
Public Const MOUSEEVENTF_MOVE = &H1 '模拟鼠标指针移动 例: mouse_event MOUSEEVENTF_LEFTDOWN,10,10,0,0
'在(10,10)模拟鼠标左键按下 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yzh8734/archive/2007/04/28/1588387.aspx方法二:namespace ClassLibrary.Hardware
...{
public class Mouse
...{
internal const byte SM_MOUSEPRESENT = 19;
internal const byte SM_CMOUSEBUTTONS = 43;
internal const byte SM_MOUSEWHEELPRESENT = 75;
internal struct POINTAPI
...{
internal int x;
internal int y;
}
internal struct RECT
...{
internal int left ;
internal int top ;
internal int right ;
internal int bottom ;
}
[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SwapMouseButton")]
internal extern static int SwapMouseButton ( int bSwap );
[System.Runtime.InteropServices.DllImport("user32" , EntryPoint="ClipCursor")]
internal extern static int ClipCursor(ref RECT lpRect);
[System.Runtime.InteropServices.DllImport( "user32.dll" , EntryPoint="GetCursorPos" )]
internal extern static int GetCursorPos( ref POINTAPI lpPoint );
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint="ShowCursor")]
internal extern static bool ShowCursor ( bool bShow ) ;
[System.Runtime.InteropServices.DllImport( "user32.dll" , EntryPoint = "EnableWindow" )]
internal extern static int EnableWindow( int hwnd , int fEnable );
[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetWindowRect")]
internal extern static int GetWindowRect( int hwnd , ref RECT lpRect ) ;
[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SetCursorPos")]
internal extern static int SetCursorPos ( int x , int y ) ;
[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetSystemMetrics")]
internal extern static int GetSystemMetrics( int nIndex );
[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SetDoubleClickTime")]
internal extern static int SetDoubleClickTime ( int wCount );
[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetDoubleClickTime")]
internal extern static int GetDoubleClickTime() ;
[System.Runtime.InteropServices.DllImport("kernel32.DLL", EntryPoint="Sleep")]
internal extern static void Sleep ( int dwMilliseconds ) ;
//得到鼠标相对与全屏的坐标,不是相对与你的Form的,且与你的分辨率有关系
public static int FullScreenPosition_X
...{
get
...{
POINTAPI _POINTAPI = new POINTAPI();
GetCursorPos ( ref _POINTAPI ); return _POINTAPI.x;
}
} public static int FullScreenPosition_Y
...{
get
...{
POINTAPI _POINTAPI = new POINTAPI();
GetCursorPos ( ref _POINTAPI ); return _POINTAPI.y;
}
}
// 隐藏 显示 鼠标
public static void Hide()
...{
ShowCursor( false ) ;
} public static void Show()
...{
ShowCursor( true ) ;
}
// 将鼠标锁定在你的Form里 不过你得将你的Form先锁了,Form Resize 就失效了
public static void Lock( System.Windows.Forms.Form ObjectForm )
...{
RECT _FormRect = new RECT (); GetWindowRect( ObjectForm.Handle.ToInt32() , ref _FormRect ); ClipCursor( ref _FormRect );
} public static void UnLock()
...{
RECT _ScreenRect = new RECT (); _ScreenRect.top = 0;
_ScreenRect.left = 0;
_ScreenRect.bottom = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom;
_ScreenRect.right = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right; ClipCursor( ref _ScreenRect );
}
// 鼠标失效,不过失效的好像不只是鼠标,小心哦
public static void Disable( System.Windows.Forms.Form ObjectForm )
...{
EnableWindow( ObjectForm.Handle.ToInt32() , 0 ) ;
}
public static void Enable( System.Windows.Forms.Form ObjectForm )
...{
EnableWindow( ObjectForm.Handle.ToInt32() , 1 ) ;
}
// 鼠标自己移动 很想动画哦 参数是2个控件的handle
// 看这个方法前,先用凉水擦把脸 反正我写的时候 头晕
public static void Move ( int From_Handle_ToInt32 , int To_Handle_ToInt32 )
...{
RECT rectFrom = new RECT () ;
RECT rectTo = new RECT () ; int i ; GetWindowRect( From_Handle_ToInt32 , ref rectFrom ) ;
GetWindowRect( To_Handle_ToInt32 , ref rectTo ) ;
if ( ( rectFrom.left + rectFrom.right ) / 2 - ( rectTo.left + rectTo.right ) / 2 > 0 )
...{
for ( i = ( rectFrom.left + rectFrom.right ) / 2 ; i >= ( rectTo.left + rectTo.right ) / 2 ; i-- )
...{
SetCursorPos ( i , ( rectFrom.top + rectFrom.bottom ) / 2) ;
Sleep ( 1 ) ;
}
}
else
...{
for ( i = ( rectFrom.left + rectFrom.right ) / 2 ; i <= ( rectTo.left + rectTo.right ) / 2 ; i++ )
...{
SetCursorPos ( i , ( rectFrom.top + rectFrom.bottom ) / 2) ;
Sleep ( 1 ) ;
}
}
if ( ( rectFrom.top + rectFrom.bottom ) / 2 - ( rectTo.top + rectTo.bottom ) / 2 > 0 )
...{
for ( i = ( rectFrom.top + rectFrom.bottom ) / 2 ; i >= ( rectTo.top + rectTo.bottom ) / 2 ; i-- )
...{
SetCursorPos ( ( rectTo.left + rectTo.right ) / 2 , i ) ;
Sleep ( 1 ) ;
}
}
else
...{
for ( i = ( rectFrom.top + rectFrom.bottom ) / 2 ; i <= ( rectTo.top + rectTo.bottom ) / 2 ; i++ )
...{
SetCursorPos ( ( rectTo.left + rectTo.right ) / 2 , i ) ;
Sleep ( 1 ) ;
}
}
} // 得到你的鼠标类型
public static string Type
...{
get
...{
if ( GetSystemMetrics( SM_MOUSEPRESENT ) == 0 )
...{
return "本计算机尚未安装鼠标" ;
}
else
...{
if ( GetSystemMetrics( SM_MOUSEWHEELPRESENT ) != 0 )
...{
return GetSystemMetrics( SM_CMOUSEBUTTONS ) + "键滚轮鼠标" ;
}
else
...{
return GetSystemMetrics( SM_CMOUSEBUTTONS ) + "键鼠标" ;
}
}
}
}
// 设置鼠标双击时间 public static void DoubleClickTime_Set( int MouseDoubleClickTime )
...{
SetDoubleClickTime( MouseDoubleClickTime );
} public static string DoubleClickTime_Get()
...{
return GetDoubleClickTime().ToString() ;
}
// 设置鼠标默认主键 我是没有见过谁左手用鼠标
public static void DefaultRightButton()
...{
SwapMouseButton ( 1 ) ;
} public static void DefaultLeftButton()
...{
SwapMouseButton ( 0 ) ;
}
}
}本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yzh8734/archive/2007/04/28/1588387.aspx