命名空间
using System.Runtime.InteropServices;
第一种方法:
声明部分
private IntPtr hwnd_win; private IntPtr hwnd_text1; private IntPtr hwnd_text2; private IntPtr hwnd_text3; const int WM_SETTEXT = 0x000C;//文本类型参数 const int BM_CLICK = 0x00F5;//单击参数 //根据窗体的标题查找窗体的句柄,第一个参数是窗体类名,一般为null。第二个参数是窗体的标题 [DllImport("user32.dll")] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); //在窗口列表中寻找与指定条件相符的第一个子窗口(包括按钮、文本框等),第一个参数是父窗体、第二个参数是从这个窗口开始查找(如IntPtr.Zero表示查找最后一个子窗体),第三个参数是窗体的注册类型,第四个参数是窗体的标题 [DllImport("user32.dll")] private static extern IntPtr FindWindowEx(IntPtr hWnd1, IntPtr hWnd2, string lpsz1, string lpsz2); //发送消息,第一个参数是接收窗体的句柄,第二个参数是消息的标识,第三个、和第四个参数是附加信息,是可选的。 [DllImport("user32.dll", EntryPoint="SendMessage" ,CharSet =CharSet.Auto)] private static extern long SendMessage(IntPtr hwnd, int wMsg, int wParam, string lParam);
向第一个文本框发送消息
private void SendToFirstText_Click(object sender, EventArgs e) { //寻找窗体句柄 hwnd_win = FindWindow(null, "登录"); hwnd_text3 = FindWindowEx(hwnd_win, IntPtr.Zero, "WindowsForms10.EDIT.app.0.378734a", null); SendMessage(hwnd_text3,WM_SETTEXT ,0 ,"这是第一个文本框"); }
向第二个文本框发送消息
private void SendToSecondText_Click(object sender, EventArgs e) { //寻找窗体句柄 hwnd_win = FindWindow(null, "登录"); hwnd_text2 = FindWindowEx(hwnd_win, hwnd_text3, "WindowsForms10.EDIT.app.0.378734a", null); SendMessage(hwnd_text2, WM_SETTEXT, 0, "这是第二个文本框"); }
向第三个文本框发送消息
private void SendToThirdText_Click(object sender, EventArgs e) { //寻找窗体句柄 hwnd_win = FindWindow(null, "登录"); hwnd_text1 = FindWindowEx(hwnd_win, hwnd_text2, "WindowsForms10.EDIT.app.0.378734a", null); SendMessage(hwnd_text1, WM_SETTEXT, 0, "这是第三个文本框"); }
第二种方法:
private IntPtr hwnd_win; const int WM_SETTEXT = 0x000C;//文本类型参数 //根据窗体的标题查找窗体的句柄,第一个参数是窗体类名,一般为null。第二个参数是窗体的标题 [DllImport("user32.dll")] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); //遍历所有子窗体 [DllImport("user32.dll")] public static extern int EnumChildWindows(int hWndParent, CallBack lpfn, int lParam); /// <summary> /// 回调业务 /// </summary> public delegate void CallBusiness(IntPtr hwnd); public delegate bool CallBack(IntPtr hwnd, int lParam); /// <summary> /// 遍历子窗体的父窗体句柄 /// </summary> public static CallBack callBackEnumChildWindows = new CallBack(ChildWindowProcess); /// <summary> /// 委托业务,需要客户端添加 /// </summary> public static CallBusiness CallFuntion; /// <summary> /// 遍历子窗体或控件 /// </summary> /// <param name="hWnd"></param> /// <param name="lParam"></param> /// <returns></returns> public static bool EnumChildWindows(IntPtr hWnd, int lParam) { EnumChildWindows(hWnd.ToInt32(), callBackEnumChildWindows, 0); return true; } /// <summary> /// 获取类名字 /// </summary> /// <param name="hwnd">需要获取类名的句柄</param> /// <param name="lpClassName">类名(执行完成以后查看)</param> /// <param name="nMaxCount">缓冲区</param> /// <returns></returns> [DllImport("user32.dll", EntryPoint = "GetClassName")] public static extern int GetClassName( IntPtr hwnd, StringBuilder lpClassName, int nMaxCount ); /// <summary> /// 遍历子控件 /// </summary> /// <param name="hwnd"></param> /// <param name="lParam"></param> /// <returns></returns> public static bool ChildWindowProcess(IntPtr hwnd, int lParam) { if (CallFuntion != null) { CallFuntion(hwnd); } return true; } private void button1_Click(object sender, EventArgs e) { List<IntPtr> list = new List<IntPtr>(); CallFuntion = delegate(IntPtr enumIntPtr) { StringBuilder s = new StringBuilder(2000); GetClassName(enumIntPtr, s, 255); if (s.ToString() == "WindowsForms10.EDIT.app.0.378734a") { list.Add(enumIntPtr); } }; //寻找窗体句柄 hwnd_win = FindWindow(null, "登录"); EnumChildWindows(hwnd_win, 0); CallFuntion = null; //第1个文本框 SendMessage(list[2], WM_SETTEXT, 0, "第一个文本框"); //第2个文本框 SendMessage(list[1], WM_SETTEXT, 0, "第二个文本框"); //第3个文本框 SendMessage(list[0], WM_SETTEXT, 0, "第三个文本框"); }