WindowsAPI.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //############################################################
  2. // https://github.com/yuzhengyang
  3. // author:yuzhengyang
  4. //############################################################
  5. using System;
  6. using System.Diagnostics;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. namespace Y.Utils.WindowsUtils.APIUtils
  10. {
  11. public class WindowsAPI
  12. {
  13. #region 系统空闲时间
  14. #region 捕获时间结构体
  15. [StructLayout(LayoutKind.Sequential)]
  16. struct LASTINPUTINFO
  17. {
  18. // 设置结构体块容量
  19. [MarshalAs(UnmanagedType.U4)]
  20. public int cbSize;
  21. // 捕获的时间
  22. [MarshalAs(UnmanagedType.U4)]
  23. public uint dwTime;
  24. }
  25. #endregion
  26. [DllImport("user32.dll")]
  27. private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
  28. /// <summary>
  29. /// 获取计算机无操作时间
  30. /// </summary>
  31. /// <returns></returns>
  32. public static long GetLastInputTime()
  33. {
  34. LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
  35. vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
  36. // 捕获时间
  37. if (!GetLastInputInfo(ref vLastInputInfo))
  38. return 0;
  39. else
  40. return Environment.TickCount - (long)vLastInputInfo.dwTime;
  41. }
  42. #endregion
  43. /// <summary>
  44. /// 获取当前窗口句柄
  45. /// </summary>
  46. /// <returns></returns>
  47. [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  48. public static extern IntPtr GetForegroundWindow();
  49. /// <summary>
  50. /// 显示窗口
  51. /// </summary>
  52. /// <param name="hwnd"></param>
  53. /// <param name="nCmdShow">0关闭 1正常显示 2最小化 3最大化</param>
  54. /// <returns></returns>
  55. [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  56. public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
  57. /// <summary>
  58. /// 获取窗口大小
  59. /// </summary>
  60. /// <param name="hWnd"></param>
  61. /// <param name="lpRect"></param>
  62. /// <returns></returns>
  63. [DllImport("user32.dll")]
  64. [return: MarshalAs(UnmanagedType.Bool)]
  65. static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
  66. #region 窗口大小结构体
  67. [StructLayout(LayoutKind.Sequential)]
  68. public struct RECT
  69. {
  70. public int Left; //最左坐标
  71. public int Top; //最上坐标
  72. public int Right; //最右坐标
  73. public int Bottom; //最下坐标
  74. }
  75. #endregion
  76. /// <summary>
  77. /// 获取窗口所在进程ID
  78. /// </summary>
  79. /// <param name="hwnd"></param>
  80. /// <param name="pid"></param>
  81. /// <returns></returns>
  82. [DllImport("user32", EntryPoint = "GetWindowThreadProcessId")]
  83. public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int pid);
  84. /// <summary>
  85. /// 获取窗体标题
  86. /// </summary>
  87. /// <param name="hWnd"></param>
  88. /// <param name="lpString"></param>
  89. /// <param name="nMaxCount"></param>
  90. /// <returns></returns>
  91. [DllImport("user32.dll")]
  92. public extern static int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
  93. [DllImport("user32.dll")]
  94. public static extern int GetWindowTextLength(IntPtr hWnd);
  95. /// <summary>
  96. /// 当前窗口标题
  97. /// </summary>
  98. /// <returns></returns>
  99. public static string GetNowWindowName()
  100. {
  101. StringBuilder windowName = new StringBuilder(GetWindowTextLength(GetForegroundWindow()) + 1);
  102. GetWindowText(GetForegroundWindow(), windowName, windowName.Capacity);
  103. return windowName.ToString() ?? "";
  104. }
  105. /// <summary>
  106. /// 当前窗口进程名
  107. /// </summary>
  108. /// <returns></returns>
  109. public static string GetNowProcessName()
  110. {
  111. int windowPid = 0;
  112. GetWindowThreadProcessId(GetForegroundWindow(), out windowPid);
  113. string processName = Process.GetProcessById(windowPid).ProcessName;
  114. return processName ?? "";
  115. }
  116. }
  117. }