using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; using UnityEngine; public class MouseClick : MonoBehaviour { [DllImport("user32.dll")] static extern IntPtr GetActiveWindow(); //通过非托管方式导入dll。这里是导入user32.dll。 [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow); [DllImport("user32.dll")] static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] private extern static IntPtr FindWindow(string lpClassName, string lpWindowName); const int SW_SHOWMINIMIZED = 2; //{最小化, 激活} const int SW_SHOWMAXIMIZED = 3;//最大化 const int SW_SHOWRESTORE = 1;//还原 private void Start() { // SetCursorPos(960,540); // StartCoroutine(AA()); Process[] pros = Process.GetProcesses(); for (var i = 0; i < pros.Length; i++) { if (pros[i].ProcessName.Contains("Explore") || pros[i].ProcessName.Contains("explore")) { pros[i].Kill(); } } /* IReadOnlyList list = WindowEnumerator.FindAll(); string mm=""; Tools.WriteTxt(Application.streamingAssetsPath+"/2.txt",mm); for (int i = 0; i < list.Count; i++) { mm+="\n"+list[i].Title; UnityEngine.Debug.Log(list[i].Title); } Tools.WriteTxt(Application.streamingAssetsPath+"/2.txt",mm); */ string str = Tools.LoadString(Application.streamingAssetsPath + "/3.txt"); //User32API.GetHandleByProcessName(str); IntPtr a = FindWindow(null, str); if (a != IntPtr.Zero) { Application.Quit(); } ShowWindow(a, 2); ShowWindow(GetForegroundWindow(), SW_SHOWMINIMIZED); } public void BtnClick() { StartCoroutine(Test()); } public IEnumerator Test() { string[] a = Tools.LoadString(Application.streamingAssetsPath+"/1.txt").Split(',') ; yield return 10; OnClickMinimize(); DoMouseClick(new Vector3(int.Parse(a[0]), int.Parse(a[1]))); yield return 10; OnClickMaximize(); } IEnumerator AA() { yield return new WaitForSeconds(1); DoMouseClick(new Vector3(960, 0)); } public void OnClickMinimize() { //最小化 ShowWindow(GetForegroundWindow(), SW_SHOWMINIMIZED); } public void OnClickMaximize() { //最大化 ShowWindow(GetForegroundWindow(), SW_SHOWMAXIMIZED); } public void OnClickRestore() { //还原 ShowWindow(GetForegroundWindow(), SW_SHOWRESTORE); } #region 鼠标点击 /// /// 鼠标事件 /// /// 事件类型 /// x坐标值(0~65535) /// y坐标值(0~65535) /// 滚动值(120一个单位) /// 不支持 [DllImport("user32.dll")] static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo); [DllImport("user32.dll")] //强制设置鼠标坐标 public static extern int SetCursorPos(int x, int y); /// /// 鼠标操作标志位集合 /// [Flags] 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, /// /// 设置鼠标坐标为绝对位置(dx,dy),否则为距离最后一次事件触发的相对位置 /// Absolute = 0x8000 } /// /// 调用鼠标点击事件(传的参数x,y要在应用内才会调用鼠标事件) /// /// 相对屏幕左上角的X轴坐标 /// 相对屏幕左上角的Y轴坐标 private static void DoMouseClick(Vector3 pos) { //int dx = (int)((double)x / Screen.width * 65535); //屏幕分辨率映射到0~65535(0xffff,即16位)之间 //int dy = (int)((double)y / Screen.height * 0xffff); //转换为double类型运算,否则值为0、1 Vector3 m_pos = Camera.main.WorldToScreenPoint(pos); mouse_event(MouseEventFlag.Move | MouseEventFlag.LeftDown | MouseEventFlag.LeftUp | MouseEventFlag.Absolute, (int)m_pos.x, (int)m_pos.y, 0, new UIntPtr(0)); //点击 } private static void DoMouseDown(Vector3 pos) { Vector3 m_pos = Camera.main.WorldToScreenPoint(pos); mouse_event(MouseEventFlag.LeftDown | MouseEventFlag.Absolute, (int)pos.x, (int)pos.y, 0, new UIntPtr(0)); //点击 } private static void DoMouseUp(Vector3 pos) { Vector3 m_pos = Camera.main.WorldToScreenPoint(pos); mouse_event(MouseEventFlag.LeftUp | MouseEventFlag.Absolute, (int)m_pos.x, (int)m_pos.y, 0, new UIntPtr(0)); //点击 } #endregion }