当前位置:网站首页>C # how to obtain DPI and real resolution (can solve the problem that has been 96)

C # how to obtain DPI and real resolution (can solve the problem that has been 96)

2022-06-23 07:03:00 Wool leek

C# obtain DPI And real resolution ( It can be solved all the time 96 The problem of )

                [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);
                const int HORZRES = 8;
                const int VERTRES = 10;
                const int LOGPIXELSX = 88;
                const int LOGPIXELSY = 90;
                const int DESKTOPVERTRES = 117;
                const int DESKTOPHORZRES = 118;
      
             /// <summary>
            ///  Get screen resolution, current physical size 
            /// </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>
           ///  The current system DPI_X  size   It's usually 96
           /// </summary>
            public static int DpiX
            {
                get
                {
                    IntPtr hdc = GetDC(IntPtr.Zero);
                    int DpiX = GetDeviceCaps(hdc, LOGPIXELSX );
                    ReleaseDC(IntPtr.Zero, hdc);
                    return DpiX;
                }
            }
            /// <summary>
            ///  The current system DPI_Y  size   It's usually 96
            /// </summary>
            public static int DpiY
            {
                get
                {
                    IntPtr hdc = GetDC(IntPtr.Zero);
                    int DpiX = GetDeviceCaps(hdc,LOGPIXELSY);
                    ReleaseDC(IntPtr.Zero, hdc);
                    return DpiX;
                }
            }
            /// <summary>
            ///  Get the real desktop resolution size 
            /// </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>
            ///  Gets the width scaling percentage  (** When you get DPI The value of has always been 96 When , The value obtained by this method can be converted to DPI,ScaleX * 96**)
            /// </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>
            ///  Gets the height scaling percentage 
            /// </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

from https://blog.csdn.net/htiannuo/article/details/77086550

原网站

版权声明
本文为[Wool leek]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230531031776.html