Example #1
0
        /// <summary>
        /// Returns the scaling of the given screen.
        /// </summary>
        /// <param name="screen">The screen which scaling should be given back.</param>
        /// <param name="dpiType">The type of dpi that should be given back..</param>
        /// <param name="dpiX">Gives the horizontal scaling back (in dpi).</param>
        /// <param name="dpiY">Gives the vertical scaling back (in dpi).</param>
        internal static void GetDpi(this Screen screen,
                                    NativeMethods.DpiType dpiType, out uint dpiX, out uint dpiY)
        {
            var point    = new Point(screen.Bounds.Left + 1, screen.Bounds.Top + 1);
            var hmonitor = NativeMethods.MonitorFromPoint(point,
                                                          NativeMethods.MONITOR_DEFAULTTONEAREST);

            switch (NativeMethods.GetDpiForMonitor(hmonitor, dpiType, out dpiX,
                                                   out dpiY).ToInt32())
            {
            case NativeMethods.S_OK: return;

            case NativeMethods.E_INVALIDARG:
                throw new ArgumentException("Unknown error. See https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510.aspx for more information.");

            default:
                throw new COMException("Unknown error. See https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510.aspx for more information.");
            }
        }