C# API 获取系统DPI缩放倍数跟分辨率大小

2021-01-24 12:12

阅读:714

标签:dev   har   text   ati   extern   details   cap   ams   profile   

原文:C# API 获取系统DPI缩放倍数跟分辨率大小

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

  • 点赞 1
  • 收藏
  • 分享
    • 文章举报
技术图片技术图片
kkbinkbi
发布了1 篇原创文章 · 获赞 0 · 访问量 7540
私信 关注

C# API 获取系统DPI缩放倍数跟分辨率大小

标签:dev   har   text   ati   extern   details   cap   ams   profile   

原文地址:https://www.cnblogs.com/lonelyxmas/p/12051986.html


评论


亲,登录后才可以留言!