WindowsAPI.cs 4.1 KB

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