c#控制windows 鼠标移动、点击和输入
- 首先可以使用ViewWizard获取窗口的类名和Title
- MouseHookHelper 鼠标相关的函数
- 主函数调用MouseHookHelper实现鼠标点击和字符输入
- 可能遇到的问题
首先可以使用ViewWizard获取窗口的类名和Title
ViewWizard下载地址:https://download.csdn.net/download/th_num/10737076
图中:窗口标题和窗口类名,可以使用下面的程序获取窗口的句柄。
MouseHookHelper 鼠标相关的函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing; //Pointnamespace FDTDControl
{public class MouseHookHelper{#region 根据句柄寻找窗体并发送消息[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]//参数1:指的是类名。参数2,指的是窗口的标题名。两者至少要知道1个public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]public static extern IntPtr SendMessage(IntPtr hwnd, uint wMsg, int wParam, string lParam);[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]public static extern IntPtr SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);#endregion#region 获取窗体位置[DllImport("user32.dll")][return: MarshalAs(UnmanagedType.Bool)]public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);[StructLayout(LayoutKind.Sequential)]public struct RECT{public int Left; //最左坐标public int Top; //最上坐标public int Right; //最右坐标public int Bottom; //最下坐标}#endregion#region 设置窗体显示形式public enum nCmdShow : uint{SW_NONE,//初始值SW_FORCEMINIMIZE,//:在WindowNT5.0中最小化窗口,即使拥有窗口的线程被挂起也会最小化。在从其他线程最小化窗口时才使用这个参数。SW_MIOE,//:隐藏窗口并激活其他窗口。SW_MAXIMIZE,//:最大化指定的窗口。SW_MINIMIZE,//:最小化指定的窗口并且激活在Z序中的下一个顶层窗口。SW_RESTORE,//:激活并显示窗口。如果窗口最小化或最大化,则系统将窗口恢复到原来的尺寸和位置。在恢复最小化窗口时,应用程序应该指定这个标志。SW_SHOW,//:在窗口原来的位置以原来的尺寸激活和显示窗口。SW_SHOWDEFAULT,//:依据在STARTUPINFO结构中指定的SW_FLAG标志设定显示状态,STARTUPINFO 结构是由启动应用程序的程序传递给CreateProcess函数的。SW_SHOWMAXIMIZED,//:激活窗口并将其最大化。SW_SHOWMINIMIZED,//:激活窗口并将其最小化。SW_SHOWMINNOACTIVATE,//:窗口最小化,激活窗口仍然维持激活状态。SW_SHOWNA,//:以窗口原来的状态显示窗口。激活窗口仍然维持激活状态。SW_SHOWNOACTIVATE,//:以窗口最近一次的大小和状态显示窗口。激活窗口仍然维持激活状态。SW_SHOWNOMAL,//:激活并显示一个窗口。如果窗口被最小化或最大化,系统将其恢复到原来的尺寸和大小。应用程序在第一次显示窗口的时候应该指定此标志。}public const int SW_HIDE = 0;public const int SW_SHOWNORMAL = 1;public const int SW_SHOWMINIMIZED = 2;public const int SW_SHOWMAXIMIZED = 3;public const int SW_MAXIMIZE = 3;public const int SW_SHOWNOACTIVATE = 4;public const int SW_SHOW = 5;public const int SW_MINIMIZE = 6;public const int SW_SHOWMINNOACTIVE = 7;public const int SW_SHOWNA = 8;public const int SW_RESTORE = 9;[DllImport("User32.dll")]public static extern bool SetForegroundWindow(IntPtr hWnd);[DllImport("User32.dll")]public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);#endregion#region 控制鼠标移动//移动鼠标 public const int MOUSEEVENTF_MOVE = 0x0001;//模拟鼠标左键按下 public const int MOUSEEVENTF_LEFTDOWN = 0x0002;//模拟鼠标左键抬起 public const int MOUSEEVENTF_LEFTUP = 0x0004;//模拟鼠标右键按下 public const int MOUSEEVENTF_RIGHTDOWN = 0x0008;//模拟鼠标右键抬起 public const int MOUSEEVENTF_RIGHTUP = 0x0010;//模拟鼠标中键按下 public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;//模拟鼠标中键抬起 public const int MOUSEEVENTF_MIDDLEUP = 0x0040;//标示是否采用绝对坐标 public const int MOUSEEVENTF_ABSOLUTE = 0x8000;[Flags]public enum MouseEventFlag : uint{Move = 0x0001,LeftDown = 0x0002,LeftUp = 0x0004,RightDown = 0x0008,RightUp = 0x0010,MiddleDown = 0x0020,MiddleUp = 0x0040,XDown = 0x0080,XUp = 0x0100,Wheel = 0x0800,VirtualDesk = 0x4000,Absolute = 0x8000}//[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)][DllImport("user32.dll")]public static extern bool SetCursorPos(int X, int Y);[DllImport("user32.dll")]public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);#endregion#region 获取坐标钩子[StructLayout(LayoutKind.Sequential)]public class POINT{public int X;public int Y;}[StructLayout(LayoutKind.Sequential)]public class MouseHookStruct{public POINT pt;public int hwnd;public int wHitTestCode;public int dwExtraInfo;}public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);//安装钩子[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);//卸载钩子[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]public static extern bool UnhookWindowsHookEx(int idHook);//调用下一个钩子[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);/// <summary> /// 获取鼠标的坐标 /// </summary> /// <param name="lpPoint">传址参数,坐标point类型</param> /// <returns>获取成功返回真</returns> [DllImport("User32")]public extern static bool GetCursorPos(ref Point lpPoint);#endregion}
}
主函数调用MouseHookHelper实现鼠标点击和字符输入
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing; //Pointnamespace FDTDControl
{class Program{private static void LeftMouseClick(MouseHookHelper.POINT pointInfo){//先移动鼠标到指定位置MouseHookHelper.SetCursorPos(pointInfo.X, pointInfo.Y);//按下鼠标左键MouseHookHelper.mouse_event(MouseHookHelper.MOUSEEVENTF_LEFTDOWN,pointInfo.X,pointInfo.Y, 0, 0);//松开鼠标左键MouseHookHelper.mouse_event(MouseHookHelper.MOUSEEVENTF_LEFTUP,pointInfo.X ,pointInfo.Y , 0, 0);}//实时打印鼠标的相对(x,y)位置private static void PrintMousePoint(int x,int y){int x1 = x;int y1 = y;while (true){Point p = new Point(1, 1);//定义存放获取坐标的point变量 MouseHookHelper.GetCursorPos(ref p);if (x1 != p.X && y1 != p.Y){System.Console.WriteLine("相对于父类窗口 dx:");System.Console.WriteLine(p.X - x);System.Console.WriteLine("相对于父类窗口 dy:");System.Console.WriteLine(p.Y - y);x1 = p.X;y1 = p.Y;}}}static void Main(string[] args){IntPtr awin = MouseHookHelper.FindWindow("HwndWrapper[DefaultDomain;;6e277d51-7be9-45b5-879e-37e195024ec9]", "FDTDControl - Microsoft Visual Studio(管理员)");if (awin == IntPtr.Zero){MessageBox.Show("没有找到窗体");return;}else{//MessageBox.Show(String.Format("{0:D}",awin));MouseHookHelper.RECT rc = new MouseHookHelper.RECT();MouseHookHelper.GetWindowRect(awin,ref rc);int width = rc.Right - rc.Left; //窗口的宽度int height = rc.Bottom - rc.Top; //窗口的高度int x = rc.Left;int y = rc.Top;MouseHookHelper.SetForegroundWindow(awin); // 设置当前窗口置前MouseHookHelper.ShowWindow(awin, MouseHookHelper.SW_SHOWNOACTIVATE);//4、5LeftMouseClick(new MouseHookHelper.POINT() //点击鼠标{X = x,Y = y,});/*IntPtr editBtn= MouseHookHelper.FindWindowEx(awin, IntPtr.Zero, null, null);MouseHookHelper.RECT rc2 = new MouseHookHelper.RECT();MouseHookHelper.GetWindowRect(editBtn, ref rc2);int x2 = rc2.Left;int y2 = rc2.Top;LeftMouseClick(new MouseHookHelper.POINT() //点击鼠标{X = x2,Y = y2,});*/// 实时获取鼠标的位置//PrintMousePoint(x, y);LeftMouseClick(new MouseHookHelper.POINT() //点击鼠标(x+466,y+466){X = x+466,Y = y+466,});// 在鼠标点击的地方打印数字SendKeys.SendWait("12");return;}}}
}
可能遇到的问题
- 找不到using System.Windows.Forms;
在上图中,引用右键点击 --》“添加引用”