得到系统当前的dpi设置值

本文介绍了一种通过C#代码获取屏幕DPI(每英寸点数)比例的方法,包括使用Windows API函数`GetDC`、`ReleaseDC`、`CreateDC`及`GetDeviceCaps`等来创建设备上下文并读取DPI信息。此外还展示了如何通过`SetProcessDPIAware`使进程DPI感知。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 public class DpiScaleHelper
    {
        [DllImport("user32.dll")]
        static extern IntPtr GetDC(IntPtr ptr);

        [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
        public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);

        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateDC(
        string lpszDriver, // driver name
        string lpszDevice, // device name
        string lpszOutput, // not used; should be NULL
        Int64 lpInitData // optional printer data
        );

        [DllImport("gdi32.dll")]
        public static extern int GetDeviceCaps(
        IntPtr hdc, // handle to DC
        int nIndex // index of capability
        );

        [DllImport("user32.dll")]
        internal static extern bool SetProcessDPIAware();

        const int DRIVERVERSION = 0;
        const int TECHNOLOGY = 2;
        const int HORZSIZE = 4;
        const int VERTSIZE = 6;
        const int HORZRES = 8;
        const int VERTRES = 10;
        const int BITSPIXEL = 12;
        const int PLANES = 14;
        const int NUMBRUSHES = 16;
        const int NUMPENS = 18;
        const int NUMMARKERS = 20;
        const int NUMFONTS = 22;
        const int NUMCOLORS = 24;
        const int PDEVICESIZE = 26;
        const int CURVECAPS = 28;
        const int LINECAPS = 30;
        const int POLYGONALCAPS = 32;
        const int TEXTCAPS = 34;
        const int CLIPCAPS = 36;
        const int RASTERCAPS = 38;
        const int ASPECTX = 40;
        const int ASPECTY = 42;
        const int ASPECTXY = 44;
        const int SHADEBLENDCAPS = 45;
        const int LOGPIXELSX = 88;
        const int LOGPIXELSY = 90;
        const int SIZEPALETTE = 104;
        const int NUMRESERVED = 106;
        const int COLORRES = 108;
        const int PHYSICALWIDTH = 110;
        const int PHYSICALHEIGHT = 111;
        const int PHYSICALOFFSETX = 112;
        const int PHYSICALOFFSETY = 113;
        const int SCALINGFACTORX = 114;
        const int SCALINGFACTORY = 115;
        const int VREFRESH = 116;
        const int DESKTOPVERTRES = 117;
        const int DESKTOPHORZRES = 118;
        const int BLTALIGNMENT = 119;

        private static double m_dipCcale = -1.0;

        /// <summary>
        /// 得到dpi的比率
        /// </summary>
        /// <returns></returns>
        public static double GetDipScale()
        {
            if (m_dipCcale == -1.0)
            {
                m_dipCcale = 1.0;
                try
                {
                    SetProcessDPIAware();  //重要
                    IntPtr screenDC = GetDC(IntPtr.Zero);
                    int dpi_x = GetDeviceCaps(screenDC, /*DeviceCap.*/LOGPIXELSX);
                    int dpi_y = GetDeviceCaps(screenDC, /*DeviceCap.*/LOGPIXELSY);
                    m_dipCcale = dpi_x / 96.0;//横向系数
                    = dpi_y / 96.0;//纵向系数
                    ReleaseDC(IntPtr.Zero, screenDC);
                }
                catch (Exception)
                {
                    //throw;
                    //没有管理员权限就获取不到了
                }
            }
            return m_dipCcale;
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值