星火微课系统客户端
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 3.6KB

4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
4 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. const int HORZRES = 8;
  24. const int VERTRES = 10;
  25. const int LOGPIXELSX = 88;
  26. const int LOGPIXELSY = 90;
  27. const int DESKTOPVERTRES = 117;
  28. const int DESKTOPHORZRES = 118;
  29. #endregion
  30. #region 属性
  31. /// <summary>
  32. /// 获取屏幕分辨率当前物理大小
  33. /// </summary>
  34. public static Size WorkingArea
  35. {
  36. get
  37. {
  38. IntPtr hdc = GetDC(IntPtr.Zero);
  39. Size size = new Size
  40. {
  41. Width = GetDeviceCaps(hdc, HORZRES),
  42. Height = GetDeviceCaps(hdc, VERTRES)
  43. };
  44. ReleaseDC(IntPtr.Zero, hdc);
  45. return size;
  46. }
  47. }
  48. /// <summary>
  49. /// 当前系统DPI_X 大小 一般为96
  50. /// </summary>
  51. public static int DpiX
  52. {
  53. get
  54. {
  55. IntPtr hdc = GetDC(IntPtr.Zero);
  56. int DpiX = GetDeviceCaps(hdc, LOGPIXELSX);
  57. ReleaseDC(IntPtr.Zero, hdc);
  58. return DpiX;
  59. }
  60. }
  61. /// <summary>
  62. /// 当前系统DPI_Y 大小 一般为96
  63. /// </summary>
  64. public static int DpiY
  65. {
  66. get
  67. {
  68. IntPtr hdc = GetDC(IntPtr.Zero);
  69. int DpiX = GetDeviceCaps(hdc, LOGPIXELSY);
  70. ReleaseDC(IntPtr.Zero, hdc);
  71. return DpiX;
  72. }
  73. }
  74. /// <summary>
  75. /// 获取真实设置的桌面分辨率大小
  76. /// </summary>
  77. public static Size DESKTOP
  78. {
  79. get
  80. {
  81. IntPtr hdc = GetDC(IntPtr.Zero);
  82. Size size = new Size
  83. {
  84. Width = GetDeviceCaps(hdc, DESKTOPHORZRES),
  85. Height = GetDeviceCaps(hdc, DESKTOPVERTRES)
  86. };
  87. ReleaseDC(IntPtr.Zero, hdc);
  88. return size;
  89. }
  90. }
  91. /// <summary>
  92. /// 获取宽度缩放百分比
  93. /// </summary>
  94. public static float ScaleX
  95. {
  96. get
  97. {
  98. IntPtr hdc = GetDC(IntPtr.Zero);
  99. /*int t = */GetDeviceCaps(hdc, DESKTOPHORZRES);
  100. /* int d = */GetDeviceCaps(hdc, HORZRES);
  101. float ScaleX = (float)GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES);
  102. ReleaseDC(IntPtr.Zero, hdc);
  103. return ScaleX;
  104. }
  105. }
  106. /// <summary>
  107. /// 获取高度缩放百分比
  108. /// </summary>
  109. public static float ScaleY
  110. {
  111. get
  112. {
  113. IntPtr hdc = GetDC(IntPtr.Zero);
  114. float ScaleY = (float)(float)GetDeviceCaps(hdc, DESKTOPVERTRES) / (float)GetDeviceCaps(hdc, VERTRES);
  115. ReleaseDC(IntPtr.Zero, hdc);
  116. return ScaleY;
  117. }
  118. }
  119. #endregion
  120. }
  121. }