WindowInfo.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace Y.Utils.WindowsAPI
  7. {
  8. public class WindowInfo
  9. {
  10. #region Windows 窗口操作
  11. /// <summary>
  12. /// 获取当前窗口句柄
  13. /// </summary>
  14. /// <returns></returns>
  15. [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  16. public static extern IntPtr GetForegroundWindow();
  17. /// <summary>
  18. /// 显示窗口
  19. /// </summary>
  20. /// <param name="hwnd"></param>
  21. /// <param name="nCmdShow">0关闭 1正常显示 2最小化 3最大化</param>
  22. /// <returns></returns>
  23. [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  24. public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
  25. /// <summary>
  26. /// 获取窗口大小
  27. /// </summary>
  28. /// <param name="hWnd"></param>
  29. /// <param name="lpRect"></param>
  30. /// <returns></returns>
  31. [DllImport("user32.dll")]
  32. [return: MarshalAs(UnmanagedType.Bool)]
  33. static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
  34. #region 窗口大小结构体
  35. [StructLayout(LayoutKind.Sequential)]
  36. public struct RECT
  37. {
  38. public int Left; //最左坐标
  39. public int Top; //最上坐标
  40. public int Right; //最右坐标
  41. public int Bottom; //最下坐标
  42. }
  43. #endregion
  44. /// <summary>
  45. /// 获取窗口所在进程ID
  46. /// </summary>
  47. /// <param name="hwnd"></param>
  48. /// <param name="pid"></param>
  49. /// <returns></returns>
  50. [DllImport("user32", EntryPoint = "GetWindowThreadProcessId")]
  51. public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int pid);
  52. /// <summary>
  53. /// 获取窗体标题
  54. /// </summary>
  55. /// <param name="hWnd"></param>
  56. /// <param name="lpString"></param>
  57. /// <param name="nMaxCount"></param>
  58. /// <returns></returns>
  59. [DllImport("user32.dll")]
  60. public extern static int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
  61. [DllImport("user32.dll")]
  62. public static extern int GetWindowTextLength(IntPtr hWnd);
  63. #endregion
  64. /// <summary>
  65. /// 当前窗口标题
  66. /// </summary>
  67. /// <returns></returns>
  68. public static string GetNowWindowName()
  69. {
  70. StringBuilder windowName = new StringBuilder(GetWindowTextLength(GetForegroundWindow()) + 1);
  71. GetWindowText(GetForegroundWindow(), windowName, windowName.Capacity);
  72. return windowName.ToString() ?? "";
  73. }
  74. /// <summary>
  75. /// 当前窗口进程名
  76. /// </summary>
  77. /// <returns></returns>
  78. public static string GetNowProcessName()
  79. {
  80. int windowPid = 0;
  81. GetWindowThreadProcessId(GetForegroundWindow(), out windowPid);
  82. string processName = Process.GetProcessById(windowPid).ProcessName;
  83. return processName ?? "";
  84. }
  85. }
  86. }