码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2019-12-17 00:36:24      阅读:462      评论:0      收藏:0      [点我收藏+]

标签: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. /// <summary>
  29. /// 获取屏幕分辨率当前物理大小
  30. /// </summary>
  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. /// <summary>
  43. /// 当前系统DPI_X 大小 一般为96
  44. /// </summary>
  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. /// <summary>
  56. /// 当前系统DPI_Y 大小 一般为96
  57. /// </summary>
  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. /// <summary>
  69. /// 获取真实设置的桌面分辨率大小
  70. /// </summary>
  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. /// <summary>
  84. /// 获取宽度缩放百分比
  85. /// </summary>
  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. /// <summary>
  99. /// 获取高度缩放百分比
  100. /// </summary>
  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 篇原创文章 · 获赞 0 · 访问量 7540

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!