Exemplo n.º 1
0
        /// <summary>
        ///     Retrieve the DPI value for the supplied window handle
        /// </summary>
        /// <param name="hWnd">IntPtr</param>
        /// <returns>dpi value</returns>
        public static uint GetDpi(IntPtr hWnd)
        {
            if (!User32Api.IsWindow(hWnd))
            {
                return(DpiHandler.DefaultScreenDpi);
            }

            // Use the easiest method, but this only works for Windows 10
            if (WindowsVersion.IsWindows10OrLater)
            {
                return(GetDpiForWindow(hWnd));
            }

            // Use the second easiest method, but this only works for Windows 8.1 or later
            if (WindowsVersion.IsWindows81OrLater)
            {
                var hMonitor = User32Api.MonitorFromWindow(hWnd, MonitorFrom.DefaultToNearest);
                // ReSharper disable once UnusedVariable
                if (GetDpiForMonitor(hMonitor, MonitorDpiType.EffectiveDpi, out var dpiX, out var dpiY))
                {
                    return(dpiX);
                }
            }

            // Fallback to the global DPI settings
            using (var hdc = SafeWindowDcHandle.FromWindow(hWnd))
            {
                if (hdc == null)
                {
                    return(DpiHandler.DefaultScreenDpi);
                }
                return((uint)Gdi32Api.GetDeviceCaps(hdc, DeviceCaps.LOGPIXELSX));
            }
        }
        /// <summary>
        ///     Create a SafeDeviceContextHandle from a hWnd
        /// </summary>
        /// <param name="hWnd">IntPtr for hWnd</param>
        /// <returns>SafeDeviceContextHandle</returns>
        public static SafeDeviceContextHandle FromHWnd(IntPtr hWnd)
        {
            if (!User32Api.IsWindow(hWnd))
            {
                return(null);
            }
            var graphics = Graphics.FromHwnd(hWnd);

            if (graphics == null)
            {
                return(null);
            }
            return(FromGraphics(graphics));
        }
Exemplo n.º 3
0
 /// <summary>
 /// Tests if the interopWindow still exists
 /// </summary>
 /// <param name="interopWindow">IInteropWindow</param>
 /// <returns>True if it's still there.
 /// Because window handles are recycled the handle could point to a different window!
 /// </returns>
 public static bool Exists(this IInteropWindow interopWindow)
 {
     return(User32Api.IsWindow(interopWindow.Handle));
 }