星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PrimaryScreen.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. static extern IntPtr GetDC(IntPtr ptr);
  14. [DllImport("gdi32.dll")]
  15. static extern int GetDeviceCaps(
  16. IntPtr hdc, // handle to DC
  17. int nIndex // index of capability
  18. );
  19. [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
  20. static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
  21. #endregion
  22. #region DeviceCaps常量
  23. /// <summary>
  24. /// 屏幕的宽度(像素);
  25. /// </summary>
  26. const int HORZRES = 8;
  27. /// <summary>
  28. /// 屏幕的高度
  29. /// </summary>
  30. const int VERTRES = 10;
  31. /// <summary>
  32. /// 沿屏幕宽度每逻辑英寸的像素数,在多显示器系统中,该值对所显示器相同
  33. /// </summary>
  34. const int LOGPIXELSX = 88;
  35. /// <summary>
  36. /// 沿屏幕高度每逻辑英寸的像素数,在多显示器系统中,该值对所显示器相同;
  37. /// </summary>
  38. const int LOGPIXELSY = 90;
  39. /// <summary>
  40. /// Windows NT:可视桌面的以像素为单位的高度。
  41. /// </summary>
  42. const int DESKTOPVERTRES = 117;
  43. /// <summary>
  44. /// DESKTOPHORZRES:Windows NT:可视桌面的以像素为单位的宽度。如果设备支持一个可视桌面或双重显示则此值可能大于VERTRES;
  45. /// </summary>
  46. 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 = */GetDeviceCaps(hdc, DESKTOPHORZRES);
  118. /* int d = */GetDeviceCaps(hdc, HORZRES);
  119. float ScaleX = (float)GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES);
  120. ReleaseDC(IntPtr.Zero, hdc);
  121. return ScaleX;
  122. }
  123. }
  124. /// <summary>
  125. /// 获取高度缩放百分比
  126. /// </summary>
  127. public static float ScaleY
  128. {
  129. get
  130. {
  131. IntPtr hdc = GetDC(IntPtr.Zero);
  132. float ScaleY = (float)(float)GetDeviceCaps(hdc, DESKTOPVERTRES) / (float)GetDeviceCaps(hdc, VERTRES);
  133. ReleaseDC(IntPtr.Zero, hdc);
  134. return ScaleY;
  135. }
  136. }
  137. #endregion
  138. }
  139. }