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

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

时间:2018-05-26 00:36:08      阅读:576      评论:0      收藏:0      [点我收藏+]

标签:https   ext   device   屏幕分辨率   ace   names   img   imp   desktop   

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

using System;  
using System.Drawing;  
using System.Runtime.InteropServices;  
  
namespace XYDES  
{  
    public class PrimaryScreen  
    {  
        #region Win32 API  
                [DllImport("user32.dll")]  
                 static extern IntPtr GetDC(IntPtr ptr);  
                [DllImport("gdi32.dll")]  
                 static extern int GetDeviceCaps(  
                IntPtr hdc, // handle to DC  
                int nIndex // index of capability  
                );  
                [DllImport("user32.dll", EntryPoint = "ReleaseDC")]  
                 static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);  
        #endregion  
        #region DeviceCaps常量  
                const int HORZRES = 8;  
                const int VERTRES = 10;  
                const int LOGPIXELSX = 88;  
                const int LOGPIXELSY = 90;  
                const int DESKTOPVERTRES = 117;  
                const int DESKTOPHORZRES = 118;  
        #endregion  
 
        #region 属性  
        /// <summary>  
        /// 获取屏幕分辨率当前物理大小  
        /// </summary>  
           public static Size WorkingArea  
            {  
                get {  
                    IntPtr hdc = GetDC(IntPtr.Zero);  
                    Size size = new Size();  
                    size.Width = GetDeviceCaps(hdc, HORZRES);  
                    size.Height = GetDeviceCaps(hdc, VERTRES);  
                    ReleaseDC(IntPtr.Zero, hdc);  
                    return size;      
                }  
            }  
        /// <summary>  
           /// 当前系统DPI_X 大小 一般为96  
        /// </summary>  
            public static int DpiX  
            {  
                get  
                {  
                    IntPtr hdc = GetDC(IntPtr.Zero);  
                    int DpiX = GetDeviceCaps(hdc, LOGPIXELSX );  
                    ReleaseDC(IntPtr.Zero, hdc);  
                    return DpiX;  
                }  
            }  
        /// <summary>  
        /// 当前系统DPI_Y 大小 一般为96  
        /// </summary>  
            public static int DpiY  
            {  
                get  
                {  
                    IntPtr hdc = GetDC(IntPtr.Zero);  
                    int DpiX = GetDeviceCaps(hdc,LOGPIXELSY);  
                    ReleaseDC(IntPtr.Zero, hdc);  
                    return DpiX;  
                }  
            }  
        /// <summary>  
            /// 获取真实设置的桌面分辨率大小  
        /// </summary>  
            public static Size DESKTOP  
            {  
                get  
                {  
                    IntPtr hdc = GetDC(IntPtr.Zero);  
                    Size size = new Size();  
                    size.Width = GetDeviceCaps(hdc,DESKTOPHORZRES );  
                    size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES);  
                    ReleaseDC(IntPtr.Zero, hdc);  
                    return size;      
                }  
            }  
  
            /// <summary>  
            /// 获取宽度缩放百分比  
            /// </summary>  
            public static float ScaleX  
            {  
                get  
                {  
                    IntPtr hdc = GetDC(IntPtr.Zero);  
                    int t = GetDeviceCaps(hdc, DESKTOPHORZRES);  
                    int d = GetDeviceCaps(hdc, HORZRES);  
                    float ScaleX = (float)GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES);   
                    ReleaseDC(IntPtr.Zero, hdc);  
                    return ScaleX;  
                }  
            }  
            /// <summary>  
            /// 获取高度缩放百分比  
            /// </summary>  
            public static float ScaleY  
            {  
                get  
                {  
                    IntPtr hdc = GetDC(IntPtr.Zero);  
                    float ScaleY = (float)(float)GetDeviceCaps(hdc, DESKTOPVERTRES) / (float)GetDeviceCaps(hdc, VERTRES);  
                    ReleaseDC(IntPtr.Zero, hdc);  
                    return ScaleY;  
                }  
            }  
       #endregion  
    }  
}  


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

标签:https   ext   device   屏幕分辨率   ace   names   img   imp   desktop   

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

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