星火微课系统客户端
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

PrimaryScreen.cs 3.5KB

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