123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- 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<WindowInfo> 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 鼠标点击
- /// <summary>
- /// 鼠标事件
- /// </summary>
- /// <param name="flags">事件类型</param>
- /// <param name="dx">x坐标值(0~65535)</param>
- /// <param name="dy">y坐标值(0~65535)</param>
- /// <param name="data">滚动值(120一个单位)</param>
- /// <param name="extraInfo">不支持</param>
- [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);
- /// <summary>
- /// 鼠标操作标志位集合
- /// </summary>
- [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,
- /// <summary>
- /// 设置鼠标坐标为绝对位置(dx,dy),否则为距离最后一次事件触发的相对位置
- /// </summary>
- Absolute = 0x8000
- }
- /// <summary>
- /// 调用鼠标点击事件(传的参数x,y要在应用内才会调用鼠标事件)
- /// </summary>
- /// <param name="x">相对屏幕左上角的X轴坐标</param>
- /// <param name="y">相对屏幕左上角的Y轴坐标</param>
- 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
- }
|