星火批注
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PrimaryScreen.cs 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Drawing;
  3. using System.Runtime.InteropServices;
  4. namespace Common.system
  5. {
  6. /// <summary>
  7. /// 获取系统DPI缩放倍数和真实大小
  8. /// </summary>
  9. public class PrimaryScreen
  10. {
  11. #region Win32 API
  12. [DllImport("user32.dll")]
  13. private static extern IntPtr GetDC(IntPtr ptr);
  14. [DllImport("gdi32.dll")]
  15. private static extern int GetDeviceCaps(
  16. IntPtr hdc, // handle to DC
  17. int nIndex // index of capability
  18. );
  19. [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
  20. private static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
  21. #endregion
  22. #region DeviceCaps常量
  23. /// <summary>
  24. /// 屏幕的宽度(像素);
  25. /// </summary>
  26. private const int HORZRES = 8;
  27. /// <summary>
  28. /// 屏幕的高度
  29. /// </summary>
  30. private const int VERTRES = 10;
  31. /// <summary>
  32. /// 沿屏幕宽度每逻辑英寸的像素数,在多显示器系统中,该值对所显示器相同
  33. /// </summary>
  34. private const int LOGPIXELSX = 88;
  35. /// <summary>
  36. /// 沿屏幕高度每逻辑英寸的像素数,在多显示器系统中,该值对所显示器相同;
  37. /// </summary>
  38. private const int LOGPIXELSY = 90;
  39. /// <summary>
  40. /// Windows NT:可视桌面的以像素为单位的高度。
  41. /// </summary>
  42. private const int DESKTOPVERTRES = 117;
  43. /// <summary>
  44. /// DESKTOPHORZRES:Windows NT:可视桌面的以像素为单位的宽度。如果设备支持一个可视桌面或双重显示则此值可能大于VERTRES;
  45. /// </summary>
  46. private const int DESKTOPHORZRES = 118;
  47. #endregion
  48. #region 属性
  49. /// <summary>
  50. /// 获取屏幕分辨率当前物理大小 设置缩放后
  51. /// </summary>
  52. public static Size WorkingArea
  53. {
  54. get
  55. {
  56. IntPtr hdc = GetDC(IntPtr.Zero);
  57. Size size = new Size
  58. {
  59. Width = GetDeviceCaps(hdc, HORZRES),
  60. Height = GetDeviceCaps(hdc, VERTRES)
  61. };
  62. ReleaseDC(IntPtr.Zero, hdc);
  63. return size;
  64. }
  65. }
  66. /// <summary>
  67. /// 当前系统DPI_X 大小 一般为96
  68. /// </summary>
  69. public static int DpiX
  70. {
  71. get
  72. {
  73. IntPtr hdc = GetDC(IntPtr.Zero);
  74. int DpiX = GetDeviceCaps(hdc, LOGPIXELSX);
  75. ReleaseDC(IntPtr.Zero, hdc);
  76. return DpiX;
  77. }
  78. }
  79. /// <summary>
  80. /// 当前系统DPI_Y 大小 一般为96
  81. /// </summary>
  82. public static int DpiY
  83. {
  84. get
  85. {
  86. IntPtr hdc = GetDC(IntPtr.Zero);
  87. int DpiX = GetDeviceCaps(hdc, LOGPIXELSY);
  88. ReleaseDC(IntPtr.Zero, hdc);
  89. return DpiX;
  90. }
  91. }
  92. /// <summary>
  93. /// 获取真实设置的桌面分辨率大小
  94. /// </summary>
  95. public static Size DESKTOP
  96. {
  97. get
  98. {
  99. IntPtr hdc = GetDC(IntPtr.Zero);
  100. Size size = new Size
  101. {
  102. Width = GetDeviceCaps(hdc, DESKTOPHORZRES),
  103. Height = GetDeviceCaps(hdc, DESKTOPVERTRES)
  104. };
  105. ReleaseDC(IntPtr.Zero, hdc);
  106. return size;
  107. }
  108. }
  109. /// <summary>
  110. /// 获取宽度缩放百分比
  111. /// </summary>
  112. public static float ScaleX
  113. {
  114. get
  115. {
  116. IntPtr hdc = GetDC(IntPtr.Zero);
  117. /*int t = */
  118. GetDeviceCaps(hdc, DESKTOPHORZRES);
  119. /* int d = */
  120. GetDeviceCaps(hdc, HORZRES);
  121. float ScaleX = GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES);
  122. ReleaseDC(IntPtr.Zero, hdc);
  123. return ScaleX;
  124. }
  125. }
  126. /// <summary>
  127. /// 获取高度缩放百分比
  128. /// </summary>
  129. public static float ScaleY
  130. {
  131. get
  132. {
  133. IntPtr hdc = GetDC(IntPtr.Zero);
  134. float ScaleY = (float)GetDeviceCaps(hdc, DESKTOPVERTRES) / GetDeviceCaps(hdc, VERTRES);
  135. ReleaseDC(IntPtr.Zero, hdc);
  136. return ScaleY;
  137. }
  138. }
  139. /// <summary>
  140. /// 缩放后的大小
  141. /// </summary>
  142. /// <returns></returns>
  143. public static SizeF ScaleScreenSize => new SizeF(DESKTOP.Width / (DpiX / 96f), DESKTOP.Height / (DpiY / 96f));
  144. /// <summary>
  145. /// 获取缩放后的工作区域大小
  146. /// </summary>
  147. public static RectangleF ScaleWorkingAreaSize
  148. {
  149. get
  150. {
  151. RectangleF rect = System.Windows.Forms.Screen.GetWorkingArea(new System.Drawing.Point((int)ScaleScreenSize.Width, (int)ScaleScreenSize.Height));
  152. return new RectangleF(((float)rect.X) / (DpiX / 96f), ((float)rect.Y) / (DpiX / 96f), ((float)rect.Width) / (DpiX / 96f), ((float)rect.Height) / (DpiY / 96f));
  153. }
  154. }
  155. #endregion
  156. }
  157. }