天高任鸟飞

more Efforts, more you get

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

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

  1. }  
  2. 转载:https://blog.csdn.net/kkbinkbi/article/details/67636930
阅读更多
个人分类: 技术类
上一篇同时匹配国内电话和手机号码的正则表达式
下一篇Sqlite中文排序方法
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭