Example #1
0
        /// <summary>
        /// Get the Effective DPI of a monitor after a user's accessibility
        /// preferences have been applied.
        /// </summary>
        /// <param name="hMonitor">The monitor.</param>
        /// <param name="dpiX">The dpi x.</param>
        /// <param name="dpiY">The dpi y.</param>
        public static void GetMonitorEffectiveDpi(IntPtr hMonitor, out uint dpiX, out uint dpiY)
        {
            dpiX = _defaultPixelsPerInch;
            dpiY = _defaultPixelsPerInch;

            ProcessDpiAwareness awareness = DpiAwareness;

            if (awareness >= ProcessDpiAwareness.PerMonitorDpiAware)
            {
                int hresult = NativeMethods.GetDpiForMonitor(hMonitor,
                                                             NativeMethods.Monitor_DPI_Type.MDT_Effective_DPI, ref dpiX, ref dpiY);

                if (hresult != 0)
                {
                    // If anything goes wrong, return a reasonable default

                    dpiX = _defaultPixelsPerInch;
                    dpiY = _defaultPixelsPerInch;
                }
            }
            else if (awareness == ProcessDpiAwareness.SystemDpiAware)
            {
                GetSystemEffectiveDpi(out dpiX, out dpiY);
            }
            else
            {
                // Use the default of 96
                return;
            }
        }
Example #2
0
        private static ProcessDpiAwareness GetDpiAwareness()
        {
            //return ProcessDpiAwareness.PerMonitorDpiAware;
            ProcessDpiAwareness result = ProcessDpiAwareness.DpiUnaware;

            try
            {
                // If at least Windows 8.1
                if (IsOsGreaterOrEqualTo(6, 3))
                {
                    int value = 0;

                    if (NativeMethods.GetProcessDpiAwareness(IntPtr.Zero, ref value) == 0)
                    {
                        result = (ProcessDpiAwareness)value;
                    }
                }
            }
            catch
            {
                result = ProcessDpiAwareness.DpiUnaware;
            }

            return(result);
        }
Example #3
0
 public static void SetDpiAware(ProcessDpiAwareness value)
 {
     if (Platform.OperatingSystem == OS.Windows)
     {
         if (Platform.OperatingSystemVersion >= Platform.Windows8_1)
         {
             var result = NativeMethods.SetProcessDpiAwareness(value);
             if (result != HRESULT.Ok)
             {
                 Console.WriteLine("Failed to set application to be DPI aware: " + result);
             }
         }
         else if (Platform.OperatingSystemVersion >= Platform.WindowsVista)
         {
             // This platform only supports system dpi awareness.
             if (!NativeMethods.SetProcessDPIAware())
             {
                 Console.WriteLine("Failed to set application to be DPI aware.");
             }
         }
         else
         {
             Console.WriteLine("This platform does not support DPI awareness.");
         }
     }
 }
Example #4
0
        public static ProcessDpiAwareness     GetProcessDpiAwareness(IntPtr handle)
        {
            ProcessDpiAwareness pda = ProcessDpiAwareness.Unaware;

            if (GetProcessDpiAwareness(handle, ref pda) == 0)
            {
                return(pda);
            }

            return(ProcessDpiAwareness.Unaware);
        }
Example #5
0
 /// <summary>
 /// Set DPI Awareness
 /// </summary>
 /// <param name="dpiAwareness"></param>
 /// <returns>true on success</returns>
 public static bool SetDpiAwareness(ProcessDpiAwareness dpiAwareness)
 {
     try
     {
         return(NativeMethods.SetProcessDpiAwareness(dpiAwareness));
     }
     catch (DllNotFoundException)
     {
         // DPI Awareness API is not available on older OS's, but they work in
         // physical pixels anyway, so we just ignore if the call fails.
     }
     return(false);
 }
Example #6
0
        /// <summary>
        /// IMPORTANT: This only works if this is called in the immediate startup code
        /// of the application. For WPF this means `static App() { }`.
        /// </summary>
        public static bool SetPerMonitorDpiAwareness(ProcessDpiAwareness type = ProcessDpiAwareness.Process_Per_Monitor_DPI_Aware)
        {
            try
            {
                // for this to work make sure [assembly: DisableDpiAwareness]
                ProcessDpiAwareness awarenessType;
                GetProcessDpiAwareness(Process.GetCurrentProcess().Handle, out awarenessType);
                var result = SetProcessDpiAwareness(type);
                GetProcessDpiAwareness(Process.GetCurrentProcess().Handle, out awarenessType);

                return(awarenessType == type);
            }
            catch
            {
                return(false);
            }
        }
Example #7
0
        /// <summary>
        ///     Attempts to set the DPI awareness of this process to PerMonitorDpiAware
        /// </summary>
        /// <returns>A value indicating whether the DPI awareness has been set to PerMonitorDpiAware.</returns>
        /// <remarks>
        ///     <para>
        ///         For this operation to succeed the host OS must be Windows 8.1 or greater, and the initial
        ///         DPI awareness must be set to DpiUnaware (apply [assembly:DisableDpiAwareness] to application assembly).
        ///     </para>
        ///     <para>
        ///         When the host OS is Windows 8 or lower, an attempt is made to set the DPI awareness to SystemDpiAware (= WPF
        ///         default). This
        ///         effectively revokes the [assembly:DisableDpiAwareness] attribute if set.
        ///     </para>
        /// </remarks>
        public static bool TrySetPerMonitorDpiAware()
        {
            ProcessDpiAwareness awareness = GetDpiAwareness();

            // initial awareness must be DpiUnaware
            if (awareness == ProcessDpiAwareness.DpiUnaware)
            {
                if (OSVersionHelper.IsWindows8Point1OrGreater)
                {
                    return(NativeMethods.SetProcessDpiAwareness(ProcessDpiAwareness.PerMonitorDpiAware) ==
                           NativeMethods.S_OK);
                }

                // use older Win32 API to set the awareness to SystemDpiAware
                return(NativeMethods.SetProcessDPIAware() == NativeMethods.S_OK);
            }

            // return true if per monitor was already enabled
            return(awareness == ProcessDpiAwareness.PerMonitorDpiAware);
        }
Example #8
0
 public static void SetDpiAware(ProcessDpiAwareness value)
 {
     if (Environment.OSVersion.Version >= new Version(6, 3) /* Windows 8.1 */)
     {
         var result = NativeMethods.SetProcessDpiAwareness(value);
         if (result != HRESULT.Ok)
         {
             Console.WriteLine("Failed to set application to be DPI aware: " + result);
         }
     }
     else if (Environment.OSVersion.Version >= new Version(6, 0) /* Windows Vista */)
     {
         // This platform only supports system dpi awareness.
         if (!NativeMethods.SetProcessDPIAware())
         {
             Console.WriteLine("Failed to set application to be DPI aware.");
         }
     }
     else
     {
         Console.WriteLine("This platform does not support DPI awareness.");
     }
 }
Example #9
0
 private static extern void GetProcessDpiAwareness(IntPtr hprocess, out ProcessDpiAwareness awareness);
Example #10
0
 private static extern bool SetProcessDpiAwareness(ProcessDpiAwareness awareness);
Example #11
0
 internal static extern bool SetProcessDpiAwareness(ProcessDpiAwareness awareness);
Example #12
0
 internal static extern int SetProcessDpiAwareness(
     [MarshalAs(UnmanagedType.U4)] ProcessDpiAwareness a);
Example #13
0
 private static extern int             GetProcessDpiAwareness(IntPtr handle, ref ProcessDpiAwareness awareness);
Example #14
0
 public static extern int             SetProcessDpiAwareness(ProcessDpiAwareness awareness);
Example #15
0
 public static extern HRESULT SetProcessDpiAwareness(ProcessDpiAwareness value);
Example #16
0
 public static extern int GetProcessDpiAwareness(IntPtr hprocess, out ProcessDpiAwareness value);
 public static extern int SetProcessDpiAwareness(ProcessDpiAwareness value);
Example #18
0
 public static extern int SetProcessDpiAwareness(ProcessDpiAwareness value);
Example #19
0
 public static extern int GetProcessDpiAwareness(IntPtr processHandle, out ProcessDpiAwareness processDpiAwareness);
 public static extern int GetProcessDpiAwareness(IntPtr hprocess, out ProcessDpiAwareness value);
Example #21
0
 private static extern void _GetProcessDpiAwareness(IntPtr hProcess, out ProcessDpiAwareness value);         // Windows 8.1 and above
 private static extern bool SetProcessDpiAwareness(ProcessDpiAwareness value);
 internal static extern uint GetProcessDpiAwareness(IntPtr process, out ProcessDpiAwareness awareness);