ScreenUtil.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Forms;
  11. namespace GeekDesk.Util
  12. {
  13. public class ScreenUtil
  14. {
  15. [DllImport("user32.dll")]
  16. static extern bool GetCursorPos(ref System.Drawing.Point lpPoint);
  17. [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
  18. public static extern int BitBlt(IntPtr hDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);
  19. [StructLayout(LayoutKind.Sequential)]
  20. private struct RECT
  21. {
  22. public int left;
  23. public int top;
  24. public int right;
  25. public int bottom;
  26. }
  27. //取得前台窗口句柄函数
  28. [DllImport("user32.dll")]
  29. private static extern IntPtr GetForegroundWindow();
  30. //取得桌面窗口句柄函数
  31. [DllImport("user32.dll")]
  32. private static extern IntPtr GetDesktopWindow();
  33. //取得Shell窗口句柄函数
  34. [DllImport("user32.dll")]
  35. private static extern IntPtr GetShellWindow();
  36. //取得窗口大小函数
  37. [DllImport("user32.dll", SetLastError = true)]
  38. private static extern int GetWindowRect(IntPtr hwnd, out RECT rc);
  39. //获取窗口标题
  40. [DllImport("user32", SetLastError = true)]
  41. private static extern int GetWindowText(
  42. IntPtr hWnd,//窗口句柄
  43. StringBuilder lpString,//标题
  44. int nMaxCount //最大值
  45. );
  46. //获取类的名字
  47. [DllImport("user32.dll")]
  48. private static extern int GetClassName(
  49. IntPtr hWnd,//句柄
  50. StringBuilder lpString, //类名
  51. int nMaxCount); //最大值
  52. /// <summary>
  53. /// 判断当前屏幕(鼠标最后活动屏幕)是否有全屏化应用
  54. /// </summary>
  55. /// <returns></returns>
  56. public static bool IsPrimaryFullScreen()
  57. {
  58. //桌面窗口句柄
  59. IntPtr desktopHandle; //Window handle for the desktop
  60. //Shell窗口句柄
  61. IntPtr shellHandle; //Window handle for the shell 因为桌面窗口和Shell窗口也是全屏,要排除在其他全屏程序之外。 //取得桌面和Shell窗口句柄
  62. desktopHandle = GetDesktopWindow();
  63. shellHandle = GetShellWindow(); //取得前台窗口句柄并判断是否全屏
  64. bool runningFullScreen = false;
  65. RECT appBounds;
  66. Rectangle screenBounds;
  67. IntPtr hWnd;
  68. //取得前台窗口
  69. hWnd = GetForegroundWindow();
  70. StringBuilder sb = new StringBuilder(256);
  71. try
  72. {
  73. GetClassName(hWnd, sb, sb.Capacity);
  74. }
  75. catch { }
  76. if (sb.ToString().ToLower().Equals("workerw")) return false;
  77. if (hWnd != null && !hWnd.Equals(IntPtr.Zero))
  78. {
  79. //判断是否桌面或shell
  80. if (!(hWnd.Equals(desktopHandle) || hWnd.Equals(shellHandle)))
  81. {
  82. //取得窗口大小
  83. GetWindowRect(hWnd, out appBounds);
  84. //判断是否全屏
  85. screenBounds = Screen.FromHandle(hWnd).Bounds;
  86. if ((appBounds.bottom - appBounds.top) == screenBounds.Height
  87. && (appBounds.right - appBounds.left) == screenBounds.Width)
  88. runningFullScreen = true;
  89. }
  90. }
  91. return runningFullScreen;
  92. }
  93. public static Color GetColorAt(System.Drawing.Point location)
  94. {
  95. Bitmap screenPixel = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
  96. using (Graphics gdest = Graphics.FromImage(screenPixel))
  97. {
  98. using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero))
  99. {
  100. IntPtr hSrcDC = gsrc.GetHdc();
  101. IntPtr hDC = gdest.GetHdc();
  102. int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, location.X, location.Y, (int)CopyPixelOperation.SourceCopy);
  103. gdest.ReleaseHdc();
  104. gsrc.ReleaseHdc();
  105. }
  106. }
  107. return screenPixel.GetPixel(0, 0);
  108. }
  109. [DllImport("gdi32")]
  110. static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
  111. public const int HORZRES = 8;
  112. public const int VERTRES = 10;
  113. public const int DESKTOPVERTRES = 117;
  114. public const int DESKTOPHORZRES = 118;
  115. /// <summary>
  116. /// 获取屏幕缩放比例
  117. /// </summary>
  118. /// <returns></returns>
  119. public static double GetScreenScalingFactor()
  120. {
  121. try
  122. {
  123. var g = Graphics.FromHwnd(IntPtr.Zero);
  124. IntPtr desktop = g.GetHdc();
  125. var physicalScreenHeight = GetDeviceCaps(desktop, (int)DESKTOPVERTRES);
  126. var screenScalingFactor =
  127. (double)physicalScreenHeight / SystemParameters.PrimaryScreenHeight;
  128. //SystemParameters.PrimaryScreenHeight;
  129. return screenScalingFactor;
  130. } catch (Exception e)
  131. {
  132. return 1;
  133. }
  134. }
  135. }
  136. }