实现的场景是人物自动打怪,聊天室刷屏引流等一系列鼠标键盘操作:
StringBuilder classname = new StringBuilder(100);
while (classname.ToString() != "Internet Explorer_Server")
{
handle = mouse.GetWindow(handle, 5);
mouse.GetClassName(handle, classname, classname.Capacity);}
mouse.click(handle, 148, 33);
mouse.input(handle, str);
这里是自己封装的一个帮助类:
public class mouse
{
#region 鼠标定义
public const int WM_KEYDOWN = 0x100;
public const int WM_KEYUP = 0x101;
public const int VK_CONTROL = 0x11;
public const int VK_F5 = 0x74;
public const int KEYEVENTF_KEYUP = 0x2;
public const int VK_MENU = 0x12;
public const int WM_SETTEXT = 0xC;
public const int WM_CLEAR = 0x303;
public const int BN_CLICKED = 0;
public const int WM_LBUTTONDOWN = 0x201;
public const int WM_LBUTTONUP = 0x202;
public const int WM_CLOSE = 0x10;
public const int WM_COMMAND = 0x111;
public const int WM_SYSKEYDOWN = 0x104;
public const int GW_HWNDNEXT = 2;
public const int WM_CLICK = 0x00F5;
public const int WM_RBUTTONDOWN = 0x204;
public const int WM_RBUTTONUP = 0x205;
public const int WM_CHAR = 0x102;
#endregion
private static Dictionary<string, byte> keycode = new Dictionary<string, byte>();
#region 键盘定义
static mouse()
{
keycode.Add("A", 65);
keycode.Add("B", 66);
keycode.Add("C", 67);
keycode.Add("D", 68);
keycode.Add("E", 69);
keycode.Add("F", 70);
keycode.Add("G", 71);
keycode.Add("H", 72);
keycode.Add("I", 73);
keycode.Add("J", 74);
keycode.Add("K", 75);
keycode.Add("L", 76);
keycode.Add("M", 77);
keycode.Add("N", 78);
keycode.Add("O", 79);
keycode.Add("P", 80);
keycode.Add("Q", 81);
keycode.Add("R", 82);
keycode.Add("S", 83);
keycode.Add("T", 84);
keycode.Add("U", 85);
keycode.Add("V", 86);
keycode.Add("W", 87);
keycode.Add("X", 88);
keycode.Add("Y", 89);
keycode.Add("Z", 90);
keycode.Add("0", 48);
keycode.Add("1", 49);
keycode.Add("2", 50);
keycode.Add("3", 51);
keycode.Add("4", 52);
keycode.Add("5", 53);
keycode.Add("6", 54);
keycode.Add("7", 55);
keycode.Add("8", 56);
keycode.Add("9", 57);
keycode.Add(".", 0x6E);
keycode.Add("LEFT", 0x25);
keycode.Add("UP", 0x26);
keycode.Add("RIGHT", 0x27);
keycode.Add("DOWN", 0x28);
keycode.Add("-", 0x6D);
}
#endregion
static uint MAKELONG(ushort x, ushort y)
{
return ((((uint)x) << 16) | y);
}
[DllImport("user32.dll",CharSet=CharSet.Auto,EntryPoint = "PostMessage")]
public static extern bool PostMessage(IntPtr hwnd, int msg, uint wParam, uint lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
public static void click(IntPtr handle,ushort x,ushort y)
{
PostMessage(handle, WM_LBUTTONDOWN, 0, MAKELONG(x, y));
PostMessage(handle, WM_LBUTTONUP, 0, MAKELONG(x, y));
}
public static void property(IntPtr handle, ushort x, ushort y)
{
PostMessage(handle, WM_RBUTTONDOWN, 0, MAKELONG(x, y));
PostMessage(handle, WM_RBUTTONUP, 0, MAKELONG(x, y));
}
public static void input(IntPtr handle, string str)
{
for (int i = 0; i < str.Length; i++)
{
PostMessage(handle, WM_CHAR, str[i], 0);
}
}
}