예제 #1
0
        /// <summary>
        /// Create a scope for the DpiAwarenessContext
        /// </summary>
        /// <param name="dpiAwarenessContext">DpiAwarenessContext</param>
        /// <param name="alternativeAwarenessContext">DpiAwarenessContext when the first isn't accepted</param>
        /// <returns>IDisposable</returns>
        public static IDisposable ScopedThreadDpiAwarenessContext(DpiAwarenessContext dpiAwarenessContext, DpiAwarenessContext?alternativeAwarenessContext = null)
        {
            DpiAwarenessContext?previousDpiAwarenessContext = null;

            if (!WindowsVersion.IsWindows10BuildOrLater(14393))
            {
                return(Disposable.Empty);
            }

            if (IsValidDpiAwarenessContext(dpiAwarenessContext))
            {
                previousDpiAwarenessContext = SetThreadDpiAwarenessContext(dpiAwarenessContext);
            }
            else if (alternativeAwarenessContext.HasValue && IsValidDpiAwarenessContext(alternativeAwarenessContext.Value))
            {
                previousDpiAwarenessContext = SetThreadDpiAwarenessContext(alternativeAwarenessContext.Value);
            }
            // Make the scope disposable
            return(Disposable.Create(() =>
            {
                if (previousDpiAwarenessContext.HasValue)
                {
                    SetThreadDpiAwarenessContext(previousDpiAwarenessContext.Value);
                }
            }));
        }
예제 #2
0
        /// <summary>
        /// Make the current process DPI Aware, this should be done via the manifest but sometimes this is not possible.
        /// </summary>
        /// <returns>bool true if it was possible to change the DPI awareness</returns>
        public static bool EnableDpiAware()
        {
            // We can only test this for Windows 8.1 or later
            if (!WindowsVersion.IsWindows81OrLater)
            {
                Log.Verbose().WriteLine("An application can only be DPI aware starting with Window 8.1 and later.");
                return(false);
            }

            if (WindowsVersion.IsWindows10BuildOrLater(15063))
            {
                if (IsValidDpiAwarenessContext(DpiAwarenessContext.PerMonitorAwareV2))
                {
                    SetProcessDpiAwarenessContext(DpiAwarenessContext.PerMonitorAwareV2);
                }
                else
                {
                    SetProcessDpiAwarenessContext(DpiAwarenessContext.PerMonitorAwareV2);
                }

                return(true);
            }
            return(SetProcessDpiAwareness(DpiAwareness.PerMonitorAware).Succeeded());
        }