zoukankan      html  css  js  c++  java
  • 获取系统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     }
    116 }

    转载于:https://www.cnblogs.com/bjxingch/articles/9960423.html

  • 相关阅读:
    22.json&pickle&shelve
    22.BASE_DIR,os,sys
    21.time和random
    21.模块的执行以及__name__
    21.python的模块(Module)和包(Package)
    21. 对文件进行查询修改等操作
    20.装饰器和函数闭包
    19.python基础试题(三)
    19.生产者消费者模型
    19.yield和send的区别
  • 原文地址:https://www.cnblogs.com/twodog/p/12135626.html
Copyright © 2011-2022 走看看