其中hwndFocus就是我们想要的,但是不能高兴太早,这个函数似乎我们用不了,因为MSDN告诉我们“This function is only available to OEMs.”。无情的打击啊,不过柳暗花明的是在Windows Mobile Test Frameworkh中,微软提供了一个.NET DLL封装了这个函数,所以……嘿嘿~对了,那个DLL叫做Microsoft.WindowsCE.Win32API.dll,下面是一段C#代码:
public static void Input(string text){ GET_FOREGROUND_INFO info = new GET_FOREGROUND_INFO(); Win32.GetForegroundInfo(out info); IntPtr focusedWindowHandle = new IntPtr(info.hwndFocus); char[] txt = text.ToCharArray(); foreach (char c in txt){ SendChar(focusedWindowHandle, WM_CHAR, c, 0); } } [DllImport("coredll.dll", SetLastError = true)] private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo); [DllImport("coredll.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)] private static extern int SendChar(IntPtr hwnd, int msg, int c, int lParam); private const int WM_CHAR = 0x0102; |
模拟鼠标输入
同键盘输入类似,模拟鼠标输入也有一个非常好的API
VOID mouse_event( DWORD dwFlags, DWORD dx, DWORD dy, DWORD dwData, DWORD dwExtraInfo ); |
用法也非常简单,只是有一点需要注意的是鼠标有自己的坐标,X,Y轴都是从0到65535,所以,如果你想在某个像素位置点击的话,别忘了转换一下坐标,下面是一个模拟鼠标拖拽的C#方法:
public static void Drag(int fromX, int fromY, int toX, int toY) { if (fromX < 0 || fromX > (SCREEN_WIDTH - 1)) if (fromY < 0 || fromY > (SCREEN_HEIGHT - 1)) if (toX < 0 || toX > (SCREEN_WIDTH - 1)) if (toY < 0 || toY > (SCREEN_HEIGHT - 1)) Point from = ScreenToMouse(fromX, fromY); MouseEvent(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, from.X, from.Y, 0, 0); } private static Point ScreenToMouse(int x, int y) { Point mouse = new Point(); return mouse; [DllImport("coredll.dll", EntryPoint = "mouse_event")] private const int MOUSEEVENTF_LEFTDOWN = 0x0002; private const int SCREEN_WIDTH = 240; |
知道了怎么拖拽,要实现点击啥的就简单了吧,哈哈~
总结完了~