WindowsAPI.cs 4.5 KB

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