ScreenAPI.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. namespace Azylee.Core.WindowsUtils.APIUtils
  8. {
  9. /// <summary>
  10. /// 屏幕分辨率接口
  11. /// </summary>
  12. public class ScreenAPI
  13. {
  14. #region Win32 API
  15. [DllImport("user32.dll")]
  16. static extern IntPtr GetDC(IntPtr ptr);
  17. [DllImport("gdi32.dll")]
  18. static extern int GetDeviceCaps(
  19. IntPtr hdc, // handle to DC
  20. int nIndex // index of capability
  21. );
  22. [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
  23. static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
  24. #endregion
  25. #region DeviceCaps常量
  26. const int HORZRES = 8;
  27. const int VERTRES = 10;
  28. const int LOGPIXELSX = 88;
  29. const int LOGPIXELSY = 90;
  30. const int DESKTOPVERTRES = 117;
  31. const int DESKTOPHORZRES = 118;
  32. #endregion
  33. #region 属性
  34. /// <summary>
  35. /// 获取屏幕分辨率当前物理大小
  36. /// </summary>
  37. public static Size WorkingArea
  38. {
  39. get
  40. {
  41. IntPtr hdc = GetDC(IntPtr.Zero);
  42. Size size = new Size();
  43. size.Width = GetDeviceCaps(hdc, HORZRES);
  44. size.Height = GetDeviceCaps(hdc, VERTRES);
  45. ReleaseDC(IntPtr.Zero, hdc);
  46. return size;
  47. }
  48. }
  49. /// <summary>
  50. /// 当前系统DPI_X 大小 一般为96
  51. /// </summary>
  52. public static int DpiX
  53. {
  54. get
  55. {
  56. IntPtr hdc = GetDC(IntPtr.Zero);
  57. int DpiX = GetDeviceCaps(hdc, LOGPIXELSX);
  58. ReleaseDC(IntPtr.Zero, hdc);
  59. return DpiX;
  60. }
  61. }
  62. /// <summary>
  63. /// 当前系统DPI_Y 大小 一般为96
  64. /// </summary>
  65. public static int DpiY
  66. {
  67. get
  68. {
  69. IntPtr hdc = GetDC(IntPtr.Zero);
  70. int DpiX = GetDeviceCaps(hdc, LOGPIXELSY);
  71. ReleaseDC(IntPtr.Zero, hdc);
  72. return DpiX;
  73. }
  74. }
  75. /// <summary>
  76. /// 获取真实设置的桌面分辨率大小
  77. /// </summary>
  78. public static Size DESKTOP
  79. {
  80. get
  81. {
  82. IntPtr hdc = GetDC(IntPtr.Zero);
  83. Size size = new Size();
  84. size.Width = GetDeviceCaps(hdc, DESKTOPHORZRES);
  85. size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES);
  86. ReleaseDC(IntPtr.Zero, hdc);
  87. return size;
  88. }
  89. }
  90. /// <summary>
  91. /// 获取宽度缩放百分比
  92. /// </summary>
  93. public static float ScaleX
  94. {
  95. get
  96. {
  97. IntPtr hdc = GetDC(IntPtr.Zero);
  98. int t = GetDeviceCaps(hdc, DESKTOPHORZRES);
  99. int d = GetDeviceCaps(hdc, HORZRES);
  100. float ScaleX = (float)GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES);
  101. ReleaseDC(IntPtr.Zero, hdc);
  102. return ScaleX;
  103. }
  104. }
  105. /// <summary>
  106. /// 获取高度缩放百分比
  107. /// </summary>
  108. public static float ScaleY
  109. {
  110. get
  111. {
  112. IntPtr hdc = GetDC(IntPtr.Zero);
  113. float ScaleY = (float)(float)GetDeviceCaps(hdc, DESKTOPVERTRES) / (float)GetDeviceCaps(hdc, VERTRES);
  114. ReleaseDC(IntPtr.Zero, hdc);
  115. return ScaleY;
  116. }
  117. }
  118. #endregion
  119. }
  120. }