最近需要模拟人员去按步骤自动操作某些程序,其中需要用到Win32 API,需要用到辅助工具Microsoft Spy++(WM_COMMAND)去获取程序操作过程的句柄或命令.
//–寻找某个窗体
IntPtr frmHandle = WIN32Util.FindWindow(“{类名}”, “{标题}”);
if (frmHandle == IntPtr.Zero)
throw new Exception(“Frame is not running.”);
//–执行某个命令
int result1 = WIN32Util.PostMessage(frmHandle, 0x0111, 0x000F, 0);
//–寻找弹出对话框
IntPtr Confirm = WIN32Util.FindWindow(“#32770”, “Confirm”);
if (Confirm == IntPtr.Zero)
throw new Exception(“Not Confirm Confirm Frame);
//–寻找子窗体
IntPtr Edit = WIN32Util.FindWindowEx(Confirm, IntPtr.Zero, “Edit”, null);
//–给子窗体的Edit自动输入字符串
string sKill = “KILLALL”;
InputStr(Edit, sKill);
—–以下是会经常用到的方法或类—————————-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/// <summary> /// 模拟键盘输出字符串 /// </summary> /// <param name="k"></param> /// <param name="Input"></param> private static void InputStr(IntPtr k, string Input) { //不能发送汉字,只能发送键盘上有的内容 也可以模拟shift+!等 byte[] ch = (ASCIIEncoding.ASCII.GetBytes(Input)); for (int i = 0; i < ch.Length; i++) { WIN32Util.SendMessage(k, WM_CHAR, ch[i], 0); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
//---经常会用到的Win32 API public class WIN32Util { [DllImport("User32.dll", EntryPoint = "FindWindow")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("User32.dll", EntryPoint = "FindWindowEx")] public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName); [DllImport("User32.dll", SetLastError = true)] public static extern int GetForegroundWindow(); [DllImport("USER32.DLL")] public static extern bool SetForegroundWindow(IntPtr hWnd); //导入为windows窗体设置焦点的方法 [DllImport("USER32.DLL")] public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo); //导入模拟键盘的方法 [DllImport("DllTest.dll")] public static extern int DllRegisterServer(); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool GetMenuItemInfo(IntPtr hMenu, uint uItem, bool fByPosition, ref MENUITEMINFO lpmii); [DllImport("user32.dll", EntryPoint = "GetLastActivePopup")] public static extern IntPtr GetLastActivePopup(IntPtr hWnd); /// <summary> /// 自定义的结构 /// </summary> public struct My_lParam { public int i; public string s; } /// <summary> /// 使用COPYDATASTRUCT来传递字符串 /// </summary> [StructLayout(LayoutKind.Sequential)] public struct COPYDATASTRUCT { public IntPtr dwData; public int cbData; [MarshalAs(UnmanagedType.LPStr)] public string lpData; } //消息发送API [DllImport("User32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage( IntPtr hWnd, // 信息发往的窗口的句柄 int Msg, // 消息ID int wParam, // 参数1 int lParam //参数2 ); //消息发送API [DllImport("User32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage( IntPtr hWnd, // 信息发往的窗口的句柄 int Msg, // 消息ID int wParam, // 参数1 ref My_lParam lParam //参数2 ); //消息发送API [DllImport("User32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage( IntPtr hWnd, // 信息发往的窗口的句柄 int Msg, // 消息ID int wParam, // 参数1 ref COPYDATASTRUCT lParam //参数2 ); //消息发送API [DllImport("User32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage( IntPtr hWnd, // 信息发往的窗口的句柄 int Msg, // 消息ID string wParam, // 参数1 string lParam //参数2 ); //消息发送API [DllImport("User32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage( IntPtr hWnd, // 信息发往的窗口的句柄 int Msg, // 消息ID IntPtr wParam, // 参数1 string lParam //参数2 ); [DllImport("User32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); //消息发送API [DllImport("User32.dll", EntryPoint = "PostMessage")] public static extern int PostMessage( IntPtr hWnd, // 信息发往的窗口的句柄 int Msg, // 消息ID int wParam, // 参数1 int lParam // 参数2 ); //消息发送API [DllImport("User32.dll", EntryPoint = "PostMessage")] public static extern int PostMessage( IntPtr hWnd, // 信息发往的窗口的句柄 int Msg, // 消息ID int wParam, // 参数1 ref My_lParam lParam //参数2 ); //异步消息发送API [DllImport("User32.dll", EntryPoint = "PostMessage")] public static extern int PostMessage( IntPtr hWnd, // 信息发往的窗口的句柄 int Msg, // 消息ID int wParam, // 参数1 ref COPYDATASTRUCT lParam // 参数2 ); [StructLayout(LayoutKind.Sequential)] public struct MENUITEMINFO { public uint cbSize; public uint fMask; public uint fType; public uint fState; public uint wID; public IntPtr hSubMenu; public IntPtr hbmpChecked; public IntPtr hbmpUnchecked; public IntPtr dwItemData; public String dwTypeData; public uint cch; public IntPtr hbmpItem; // Return the size of the structure public static uint sizeOf { get { return (uint)Marshal.SizeOf(typeof(MENUITEMINFO)); } } } } |
I got what you intend, appreciate it for posting .Woh I am lucky to find this website through google. I was walking down the street wearing glasses when the prescription ran out. by Steven Wright. gbedabedeega
Thanks
Your goal is to breed all the different dragons available bkfbaafegkkg
Thanks
Excellent post. I was checking continuously this blog and I'm impressed! Very useful info particularly the last part bbceebbceggdgdfe
Thanks
Thanks for the sensible critique. Me & my neighbor were just preparing to do a little research about this. We got a grab a book from our area library but I think I learned more clear from this post. I am very glad to see such wonderful info being shared freely out there. ccfgkdadbbgf
Thanks
if making snowfall leaps Hermes purses And totes operating 3ounce throwaway duplicate Hermes cups of coffee or even a conforms icle, pour the juices a mixture on the road to these kind of people until it is they have been perfect possessions wall plug ecommerce better than nearly full. edebgedcabdf
Thanks
Very efficiently written information. It will be beneficial to everyone who employess it, including myself. Keep up the good work for sure i will check out more posts. gkgegegeeddfgdee
Thanks