MouseClick.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Runtime.InteropServices;
  6. using UnityEngine;
  7. public class MouseClick : MonoBehaviour
  8. {
  9. [DllImport("user32.dll")]
  10. static extern IntPtr GetActiveWindow();
  11. //通过非托管方式导入dll。这里是导入user32.dll。
  12. [DllImport("user32.dll")]
  13. public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
  14. [DllImport("user32.dll")]
  15. static extern IntPtr GetForegroundWindow();
  16. [DllImport("user32.dll")]
  17. private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
  18. const int SW_SHOWMINIMIZED = 2; //{最小化, 激活}
  19. const int SW_SHOWMAXIMIZED = 3;//最大化
  20. const int SW_SHOWRESTORE = 1;//还原
  21. private void Start()
  22. {
  23. // SetCursorPos(960,540);
  24. // StartCoroutine(AA());
  25. Process[] pros = Process.GetProcesses();
  26. for (var i = 0; i < pros.Length; i++)
  27. {
  28. if (pros[i].ProcessName.Contains("Explore") || pros[i].ProcessName.Contains("explore"))
  29. {
  30. pros[i].Kill();
  31. }
  32. }
  33. /* IReadOnlyList<WindowInfo> list = WindowEnumerator.FindAll();
  34. string mm="";
  35. Tools.WriteTxt(Application.streamingAssetsPath+"/2.txt",mm);
  36. for (int i = 0; i < list.Count; i++)
  37. {
  38. mm+="\n"+list[i].Title;
  39. UnityEngine.Debug.Log(list[i].Title);
  40. }
  41. Tools.WriteTxt(Application.streamingAssetsPath+"/2.txt",mm);
  42. */
  43. string str = Tools.LoadString(Application.streamingAssetsPath + "/3.txt");
  44. //User32API.GetHandleByProcessName(str);
  45. IntPtr a = FindWindow(null, str);
  46. if (a != IntPtr.Zero)
  47. {
  48. Application.Quit();
  49. }
  50. ShowWindow(a, 2);
  51. ShowWindow(GetForegroundWindow(), SW_SHOWMINIMIZED);
  52. }
  53. public void BtnClick()
  54. {
  55. StartCoroutine(Test());
  56. }
  57. public IEnumerator Test()
  58. {
  59. string[] a = Tools.LoadString(Application.streamingAssetsPath+"/1.txt").Split(',') ;
  60. yield return 10;
  61. OnClickMinimize();
  62. DoMouseClick(new Vector3(int.Parse(a[0]), int.Parse(a[1])));
  63. yield return 10;
  64. OnClickMaximize();
  65. }
  66. IEnumerator AA()
  67. {
  68. yield return new WaitForSeconds(1);
  69. DoMouseClick(new Vector3(960, 0));
  70. }
  71. public void OnClickMinimize()
  72. {
  73. //最小化
  74. ShowWindow(GetForegroundWindow(), SW_SHOWMINIMIZED);
  75. }
  76. public void OnClickMaximize()
  77. {
  78. //最大化
  79. ShowWindow(GetForegroundWindow(), SW_SHOWMAXIMIZED);
  80. }
  81. public void OnClickRestore()
  82. {
  83. //还原
  84. ShowWindow(GetForegroundWindow(), SW_SHOWRESTORE);
  85. }
  86. #region 鼠标点击
  87. /// <summary>
  88. /// 鼠标事件
  89. /// </summary>
  90. /// <param name="flags">事件类型</param>
  91. /// <param name="dx">x坐标值(0~65535)</param>
  92. /// <param name="dy">y坐标值(0~65535)</param>
  93. /// <param name="data">滚动值(120一个单位)</param>
  94. /// <param name="extraInfo">不支持</param>
  95. [DllImport("user32.dll")]
  96. static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
  97. [DllImport("user32.dll")] //强制设置鼠标坐标
  98. public static extern int SetCursorPos(int x, int y);
  99. /// <summary>
  100. /// 鼠标操作标志位集合
  101. /// </summary>
  102. [Flags]
  103. enum MouseEventFlag : uint
  104. {
  105. Move = 0x0001,
  106. LeftDown = 0x0002,
  107. LeftUp = 0x0004,
  108. RightDown = 0x0008,
  109. RightUp = 0x0010,
  110. MiddleDown = 0x0020,
  111. MiddleUp = 0x0040,
  112. XDown = 0x0080,
  113. XUp = 0x0100,
  114. Wheel = 0x0800,
  115. VirtualDesk = 0x4000,
  116. /// <summary>
  117. /// 设置鼠标坐标为绝对位置(dx,dy),否则为距离最后一次事件触发的相对位置
  118. /// </summary>
  119. Absolute = 0x8000
  120. }
  121. /// <summary>
  122. /// 调用鼠标点击事件(传的参数x,y要在应用内才会调用鼠标事件)
  123. /// </summary>
  124. /// <param name="x">相对屏幕左上角的X轴坐标</param>
  125. /// <param name="y">相对屏幕左上角的Y轴坐标</param>
  126. private static void DoMouseClick(Vector3 pos)
  127. {
  128. //int dx = (int)((double)x / Screen.width * 65535); //屏幕分辨率映射到0~65535(0xffff,即16位)之间
  129. //int dy = (int)((double)y / Screen.height * 0xffff); //转换为double类型运算,否则值为0、1
  130. Vector3 m_pos = Camera.main.WorldToScreenPoint(pos);
  131. mouse_event(MouseEventFlag.Move | MouseEventFlag.LeftDown | MouseEventFlag.LeftUp | MouseEventFlag.Absolute, (int)m_pos.x, (int)m_pos.y, 0, new UIntPtr(0)); //点击
  132. }
  133. private static void DoMouseDown(Vector3 pos)
  134. {
  135. Vector3 m_pos = Camera.main.WorldToScreenPoint(pos);
  136. mouse_event(MouseEventFlag.LeftDown | MouseEventFlag.Absolute, (int)pos.x, (int)pos.y, 0, new UIntPtr(0)); //点击
  137. }
  138. private static void DoMouseUp(Vector3 pos)
  139. {
  140. Vector3 m_pos = Camera.main.WorldToScreenPoint(pos);
  141. mouse_event(MouseEventFlag.LeftUp | MouseEventFlag.Absolute, (int)m_pos.x, (int)m_pos.y, 0, new UIntPtr(0)); //点击
  142. }
  143. #endregion
  144. }