WindowMaxAndMin.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. using WindowsHandler;
  8. public class WindowMaxAndMin : MonoBehaviour
  9. {
  10. [DllImport("user32.dll")]
  11. public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
  12. [DllImport("user32.dll")]
  13. static extern IntPtr GetForegroundWindow();
  14. // 查找窗口句柄
  15. [DllImport("user32.dll")]
  16. static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  17. const int SW_SHOWMINIMIZED = 2; //{最小化, 激活}
  18. const int SW_SHOWMAXIMIZED = 3;//最大化
  19. const int SW_SHOWRESTORE = 1;//还原
  20. Process[] pros;
  21. private void Start()
  22. {
  23. // OnClickMinimize();
  24. }
  25. IEnumerator AA()
  26. {
  27. yield return new WaitForSeconds(5);
  28. }
  29. [ContextMenu("111")]
  30. public void OnClickMinimize()
  31. {
  32. var windows = WindowEnumerator.FindAll(_ => true);
  33. for (int i = 0; i < windows.Count; i++)
  34. {
  35. var window = windows[i];
  36. if (window.Title.Contains("o"))
  37. UnityEngine.Debug.Log($@"{i.ToString().PadLeft(3, ' ')}. {window.Title}
  38. {window.Bounds.X}, {window.Bounds.Y}, {window.Bounds.Width}, {window.Bounds.Height}");
  39. }
  40. IntPtr handle = FindWindow(null, "Goole_Chrome");
  41. if (handle == IntPtr.Zero)
  42. {
  43. UnityEngine.Debug.Log(1);
  44. }
  45. else
  46. {
  47. UnityEngine.Debug.Log(2);
  48. return;
  49. }
  50. //最小化
  51. // ShowWindow(GetForegroundWindow(), SW_SHOWMINIMIZED);
  52. }
  53. public void OnClickMaximize()
  54. {
  55. //最大化
  56. ShowWindow(GetForegroundWindow(), SW_SHOWMAXIMIZED);
  57. }
  58. public void OnClickRestore()
  59. {
  60. //还原
  61. ShowWindow(GetForegroundWindow(), SW_SHOWRESTORE);
  62. }
  63. }