/// <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; } }
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); }
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."); } } }
public static ProcessDpiAwareness GetProcessDpiAwareness(IntPtr handle) { ProcessDpiAwareness pda = ProcessDpiAwareness.Unaware; if (GetProcessDpiAwareness(handle, ref pda) == 0) { return(pda); } return(ProcessDpiAwareness.Unaware); }
/// <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); }
/// <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); } }
/// <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); }
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."); } }
private static extern void GetProcessDpiAwareness(IntPtr hprocess, out ProcessDpiAwareness awareness);
private static extern bool SetProcessDpiAwareness(ProcessDpiAwareness awareness);
internal static extern bool SetProcessDpiAwareness(ProcessDpiAwareness awareness);
internal static extern int SetProcessDpiAwareness( [MarshalAs(UnmanagedType.U4)] ProcessDpiAwareness a);
private static extern int GetProcessDpiAwareness(IntPtr handle, ref ProcessDpiAwareness awareness);
public static extern int SetProcessDpiAwareness(ProcessDpiAwareness awareness);
public static extern HRESULT SetProcessDpiAwareness(ProcessDpiAwareness value);
public static extern int GetProcessDpiAwareness(IntPtr hprocess, out ProcessDpiAwareness value);
public static extern int SetProcessDpiAwareness(ProcessDpiAwareness value);
public static extern int GetProcessDpiAwareness(IntPtr processHandle, out ProcessDpiAwareness processDpiAwareness);
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);