[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
[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);
const int WM_GETTEXT = 0x000D;
const int WM_SETTEXT = 0x000C;
const int WM_CLICK = 0x00F5;
private void button3_Click(object sender, EventArgs e)
{
int retval = 0; //增加一个返回值用来判断操作是否成功
//下面的这些参数都可以用Spy++查到
//string lpszParentClass = "#32770"; //整个窗口的类名
string lpszParentWindow = "Form1"; //窗口标题
string lpszClass = "WindowsForms10.EDIT.app.0.b7ab7b"; //需要查找的子窗口的类名,也就是输入框
string lpszClass_Submit = "WindowsForms10.BUTTON.app.0.b7ab7b"; //需要查找的Button的类名
string lpszName_Submit = "确定"; //需要查找的Button的标题
string text = "";
IntPtr ParenthWnd = new IntPtr(0);
IntPtr EdithWnd = new IntPtr(0);
//查到窗体,得到整个窗体
ParenthWnd = FindWindow(null, lpszParentWindow);
//判断这个窗体是否有效
if (!ParenthWnd.Equals(IntPtr.Zero))
{
//得到User Name这个子窗体,并设置其内容
EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, lpszClass, "");
if (!EdithWnd.Equals(IntPtr.Zero))
{
text = "Book";
//调用SendMessage方法设置其内容
SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, text);
retval++;
}
//得到Password这个子窗体,并设置其内容
EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, lpszClass, "");
if (!EdithWnd.Equals(IntPtr.Zero))
{
text = "ITest";
SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, text);
retval++;
}
//得到Domain这个子窗体,并设置其内容
EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, lpszClass, "");
if (!EdithWnd.Equals(IntPtr.Zero))
{
text = "Ilove";
SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, text);
retval++;
}
//得到Button这个子窗体,并触发它的Click事件
EdithWnd = FindWindowEx(ParenthWnd,
(IntPtr)0, lpszClass_Submit, lpszName_Submit);
if (!EdithWnd.Equals(IntPtr.Zero))
{
SendMessage(EdithWnd, WM_CLICK, (IntPtr)0, "0");
retval++;
}
}
文章出处:DIY部落( http://www.diybl.com/course/3_program/vc/vc_js/20090727/168175.html)