2010-05-06 16:43:37 阅读7 评论0 字号:大中小
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;
using System.Reflection;
using System.Diagnostics;
using System.IO;
namespace Csharp
{
public class OutFrm
{
#region Dll Import 添加对API的引用
//获取主窗口句柄的API函数
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
//获取子窗口句柄的API函数
[DllImport("User32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
//向指定句柄的窗口发送消息
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
const int WM_CHAR = 0x0102; //字符
const int WM_GETTEXT = 0x000D; //得到文本
const int WM_SETTEXT = 0x000C; //设置文本
const int WM_CLICK = 0x00F5; //单击常量
const int WM_CLOSE = 0x0010; //关闭窗口
const int WM_LBUTTONDBLCLK = 0x0203; //双击
const int LB_SETSEL = 0x0185; //用于单选
const int LB_FINDSTRING = 0x018F; //用与查找字符串
const int SW_MAXIMIZE = 3; //9为最小化
#endregion
#region 声明字段 用Spy++查到的
//下面的这些参数都可以用Spy++查到
public string lpszParentClass = "WindowsForms10.Window.8.app.0.378734a"; //整个窗口的类名
public string lpszParentWindow = "Form1"; //窗口标题
public string lpszClass = "WindowsForms10.EDIT.app.0.378734a"; //需要查找的子窗口的类名,也就是输入框
public string lpszClass_Submit = "WindowsForms10.BUTTON.app.0.378734a"; //需要查找的Button的类名
public string lpszName_Submit = "登陆"; //需要查找的Button的标题
public string lpszClass_Panel = "WindowsForms10.Window.8.app.0.378734a"; //多了一个类,输入框和按钮都放在这个类上的!
string text = "";
IntPtr ParenthWnd = new IntPtr(0);
IntPtr ParenthWnd_sub = new IntPtr(0);
IntPtr EdithWnd = new IntPtr(0);
//下面的参数设定登陆用户名及密码
string UserID = "ueser";
string Pwd = "pwd";
#endregion
#region 构造函数
public OutFrm()
{
//InitializeComponent();
}
#endregion
#region SetWindow 核心部分,查找窗体并对它进行登陆操作
public int SetWindow()
{
int retval = 0; //增加一个返回值用来判断操作是否成功
//查到窗体,得到整个窗体
ParenthWnd = FindWindow(lpszParentClass, lpszParentWindow);
ParenthWnd_sub = FindWindowEx(ParenthWnd, EdithWnd, lpszClass_Panel, "");
//判断这个窗体是否有效
if (!ParenthWnd_sub.Equals(IntPtr.Zero))
{
//得到UserName这个子窗体,并设置其内容
EdithWnd = FindWindowEx(ParenthWnd_sub, EdithWnd, lpszClass, "");
if (!EdithWnd.Equals(IntPtr.Zero))
{
text = UserID;
//调用SendMessage方法设置其内容
SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, text);
retval++;
}
//得到Password这个子窗体,并设置其内容
EdithWnd = FindWindowEx(ParenthWnd_sub, EdithWnd, lpszClass, "");
if (!EdithWnd.Equals(IntPtr.Zero))
{
text = Pwd;
SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, text);
retval++;
}
//得到Button这个子窗体,并触发它的Click事件
EdithWnd = (IntPtr)0;
EdithWnd = FindWindowEx(ParenthWnd_sub, EdithWnd, lpszClass_Submit, lpszName_Submit);
if (!EdithWnd.Equals(IntPtr.Zero))
{
SendMessage(EdithWnd, WM_CLICK, (IntPtr)0, "0");
retval++;
}
}
return retval;
}
#endregion
}
}