/*整个Windows编程的基础。一个句柄是指使用的一个唯一的整数值,即一个4字节(64位程序中为8字节)长的数值,来标识应用程序中的不同对象和同类中的不同的实例,诸如,一个窗口,按钮,图标,滚动条,输出设备,控件或者文件等。应用程序能够通过句柄访问相应的对象的信息,但是句柄不是指针,程序不能利用句柄来直接阅读文件中的信息。如果句柄不在I/O文件中,它是毫无用处的。 句柄是Windows用来标志应用程序中建立的或是使用的唯一整数,Windows大量使用了句柄来标识对象。*/
[DllImport("User32.dll", EntryPoint = "FindWindow")] private static extern IntPtr FindWindow(string lpClassName,string lpWindowName); [DllImport("user32.dll", EntryPoint = "FindWindowEx",SetLastError = true)] 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; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int retval = 0; //增加一个返回值用来判断操作是否成功 //string lpszParentClass = "#32770"; //整个窗口的类名 string lpszParentWindow = "Form1"; //窗口标题 string lpszClass = "WindowsForms10.EDIT.app.0.b7ab7b"; //需要查找的子窗口的类名,也就是输入框 //string lpszClass = "Edit"; string lpszClass_Submit = "WindowsForms10.BUTTON.app.0.b7ab7b"; //需要查找的Button的类名 //string lpszClass_Submit = "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)) { //得到Form1这个子窗体的文本框,并设置其内容 EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, lpszClass, ""); [color=#FF0000]这里获取到的EdithWnd始终为0;[/color] if (!EdithWnd.Equals(IntPtr.Zero)) { text = "test1"; //调用SendMessage方法设置其内容 SendMessage(EdithWnd, WM_SETTEXT, IntPtr.Zero, 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++; } } }
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx",SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter, stringlpszClass, 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;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int retval = 0; //增加一个返回值用来判断操作是否成功
//string lpszParentClass = "#32770"; //整个窗口的类名
string lpszParentWindow = "Form1"; //窗口标题
string lpszClass = "WindowsForms10.EDIT.app.0.b7ab7b"; //需要查找的子窗口的类名,也就是输入框
//string lpszClass = "Edit";
string lpszClass_Submit = "WindowsForms10.BUTTON.app.0.b7ab7b"; //需要查找的Button的类名
//string lpszClass_Submit = "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))
{
//得到Form1这个子窗体的文本框,并设置其内容
EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, lpszClass, ""); [color=#FF0000]这里获取到的EdithWnd始终为0;[/color]
if (!EdithWnd.Equals(IntPtr.Zero))
{
text = "test1";
//调用SendMessage方法设置其内容
SendMessage(EdithWnd, WM_SETTEXT, IntPtr.Zero, 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++;
}
}
}