需求是这样,有个程序界面咱们须要经过本身的程序持续输入数据,界面如图。c#
能够得到控件的句柄而用钩子写入值。这里须要用到spy++工具。在VS的工具下有个spy++工具,打开以下图工具
经过这个工具能够得到窗体的句柄,固然这里得到的句柄只能用于测试,由于.net开发的程序窗体每次打开句柄都会变,都须要从新得到。这个工具的用处在于找一个控件的前一句柄控件和后一句柄控件。测试
//寻找目标进程窗口 [DllImport("USER32.DLL")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("USER32.DLL", EntryPoint = "FindWindowEx", SetLastError = true)] public static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter,string lpszClass, string lpszWindow); //设置进程窗口到最前 [DllImport("USER32.DLL")] public static extern bool SetForegroundWindow(IntPtr hWnd); //模拟键盘事件 [DllImport("USER32.DLL")] public static extern void keybd_event(Byte bVk, Byte bScan, Int32 dwFlags, Int32 dwExtraInfo); public delegate bool CallBack(IntPtr hwnd, int lParam); [DllImport("USER32.DLL")] public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam); //给CheckBox发送信息 [DllImport("USER32.DLL", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)] public static extern int SendMessage(IntPtr hwnd, UInt32 wMsg, int wParam, int lParam); //给Text发送信息 [DllImport("USER32.DLL", EntryPoint = "SendMessage")] private static extern int SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, string lParam); [DllImport("USER32.DLL")] public static extern IntPtr GetWindow(IntPtr hWnd, int wCmd);
/// <summary> /// 查找窗体上控件句柄 /// </summary> /// <param name="hwnd">父窗体句柄</param> /// <param name="lpszWindow">控件标题(Text)</param> /// <param name="bChild">设定是否在子窗体中查找</param> /// <returns>控件句柄,没找到返回IntPtr.Zero</returns> private static IntPtr FindWindowEx(IntPtr hwnd, string lpszWindow, bool bChild) { IntPtr iResult = IntPtr.Zero; // 首先在父窗体上查找控件 iResult = FindWindowEx(hwnd, 0, null, lpszWindow); // 若是找到直接返回控件句柄 if (iResult != IntPtr.Zero) return iResult; // 若是设定了不在子窗体中查找 if (!bChild) return iResult; // 枚举子窗体,查找控件句柄 int i = EnumChildWindows( hwnd, (h, l) => { IntPtr f1 = FindWindowEx(h, 0, null, lpszWindow); if (f1 == IntPtr.Zero) return true; else { iResult = f1; return false; } }, 0); // 返回查找结果 return iResult; } /// <summary> /// 输入回车 /// </summary> private static void PrintEnter() { keybd_event(Convert.ToByte(13), 0, 0, 0); keybd_event(Convert.ToByte(13), 0, 2, 0); }
IntPtr wcHandle = FindWindow(null, "窗体名称"); if (wcHandle != IntPtr.Zero) { //设置游戏窗口到最前 SetForegroundWindow(wcHandle); }
uint BM_GETCHECK = 0xF0; uint BST_CHECKED = 0xF1; IntPtr weightIntPtr = FindWindowEx(wcHandle, "输入重量[&H]", true); if (weightIntPtr != IntPtr.Zero) { //得到Checkbox值 int i = SendMessage(weightIntPtr, BM_GETCHECK, 0, 0); //更改CheckBox值 SendMessage(weightIntPtr, BST_CHECKED, 1, 0); }
public enum WindowSearch { GW_HWNDFIRST = 0, //同级别第一个 GW_HWNDLAST = 1, //同级别最后一个 GW_HWNDNEXT = 2, //同级别下一个 GW_HWNDPREV = 3, //同级别上一个 GW_OWNER = 4, //属主窗口 GW_CHILD = 5 //子窗口}获取与指定窗口具备指定关系的窗口的句柄 } IntPtr waybill = GetWindow(waybillIntPtr, (int)WindowSearch.GW_HWNDNEXT); SendMessage(waybill, WM_SETTEXT, IntPtr.Zero, waybillValue);
这里完成了咱们的需求,改变了窗体中选择框和Text文本框的值。ui