WindowsAPI.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. namespace Y.Utils.ComputerUtils
  5. {
  6. public class WindowsAPI
  7. {
  8. #region 系统空闲时间
  9. #region 捕获时间结构体
  10. [StructLayout(LayoutKind.Sequential)]
  11. struct LASTINPUTINFO
  12. {
  13. // 设置结构体块容量
  14. [MarshalAs(UnmanagedType.U4)]
  15. public int cbSize;
  16. // 捕获的时间
  17. [MarshalAs(UnmanagedType.U4)]
  18. public uint dwTime;
  19. }
  20. #endregion
  21. [DllImport("user32.dll")]
  22. private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
  23. /// <summary>
  24. /// 获取计算机无操作时间
  25. /// </summary>
  26. /// <returns></returns>
  27. public static long GetLastInputTime()
  28. {
  29. LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
  30. vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
  31. // 捕获时间
  32. if (!GetLastInputInfo(ref vLastInputInfo))
  33. return 0;
  34. else
  35. return Environment.TickCount - (long)vLastInputInfo.dwTime;
  36. }
  37. #endregion
  38. #region Windows 窗口操作
  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. #endregion
  92. }
  93. }