WindowsAPI.cs 3.4 KB

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