public static DPI_AWARENESS GetProcessDpiAwareness()
        {
            DPI_AWARENESS result = DPI_AWARENESS.DPI_AWARENESS_INVALID;
            IntPtr        pid    = Process.GetCurrentProcess().Handle;

            try
            {
                _GetProcessDpiAwareness(pid, out result);
            }
            catch (Exception)
            {
            }
            return(result);
        }
        /*
         *      // Dpi awareness context values. Matching windows values.
         *      public static readonly DPI_AWARENESS_CONTEXT DPI_AWARENESS_CONTEXT_UNAWARE = (-1);
         *      public static readonly DPI_AWARENESS_CONTEXT DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = (-2);
         *      public static readonly DPI_AWARENESS_CONTEXT DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = (-3);
         *      public static readonly DPI_AWARENESS_CONTEXT DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = (-4);
         *      public static readonly DPI_AWARENESS_CONTEXT DPI_AWARENESS_CONTEXT_UNSPECIFIED = (0);*/

        internal static DpiAwarenessContext GetDpiAwarenessContextForWindow(IntPtr hWnd)
        {
            DpiAwarenessContext dpiAwarenessContext = DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED;

            if (OsVersion.IsWindows10_1607OrGreater)
            {
                // Works only >= Windows 10/1607
                IntPtr        awarenessContext = GetWindowDpiAwarenessContext(hWnd);
                DPI_AWARENESS awareness        = GetAwarenessFromDpiAwarenessContext(awarenessContext);
                dpiAwarenessContext = ConvertToDpiAwarenessContext(awareness);
            }

            return(dpiAwarenessContext);
        }
Exemple #3
0
        /*
         *      // Dpi awareness context values. Matching windows values.
         *      public static readonly DPI_AWARENESS_CONTEXT DPI_AWARENESS_CONTEXT_UNAWARE = (-1);
         *      public static readonly DPI_AWARENESS_CONTEXT DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = (-2);
         *      public static readonly DPI_AWARENESS_CONTEXT DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = (-3);
         *      public static readonly DPI_AWARENESS_CONTEXT DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = (-4);
         *      public static readonly DPI_AWARENESS_CONTEXT DPI_AWARENESS_CONTEXT_UNSPECIFIED = (0);*/

        internal static DpiAwarenessContext GetDpiAwarenessContextForWindow(IntPtr hWnd)
        {
            DpiAwarenessContext dpiAwarenessContext = DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED;

            if (ApiHelper.IsApiAvailable(ExternDll.User32, "GetWindowDpiAwarenessContext") &&
                ApiHelper.IsApiAvailable(ExternDll.User32, "GetAwarenessFromDpiAwarenessContext"))
            {
                // Works only >= Windows 10/1607
                IntPtr        awarenessContext = GetWindowDpiAwarenessContext(hWnd);
                DPI_AWARENESS awareness        = GetAwarenessFromDpiAwarenessContext(awarenessContext);
                dpiAwarenessContext = ConvertToDpiAwarenessContext(awareness);
            }

            return(dpiAwarenessContext);
        }
        private static DpiAwarenessContext ConvertToDpiAwarenessContext(DPI_AWARENESS dpiAwareness)
        {
            switch (dpiAwareness)
            {
            case DPI_AWARENESS.DPI_AWARENESS_UNAWARE:
                return(DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNAWARE);

            case DPI_AWARENESS.DPI_AWARENESS_SYSTEM_AWARE:
                return(DpiAwarenessContext.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);

            case DPI_AWARENESS.DPI_AWARENESS_PER_MONITOR_AWARE:
                return(DpiAwarenessContext.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);

            default:
                return(DpiAwarenessContext.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
            }
        }
Exemple #5
0
        protected override void WndProc(ref Message message)
        {
            if (this.IsHandleCreated == true &&
                this.DPIAware == true &&
                message.Msg == 0x02E0) // WM_DPICHANGED
            {
                uint          uDpi         = 96;
                DPI_AWARENESS dpiAwareness = GetAwarenessFromDpiAwarenessContext(GetThreadDpiAwarenessContext());
                switch (dpiAwareness)
                {
                case DPI_AWARENESS.SystemAware:
                    uDpi = GetDpiForSystem();
                    break;

                case DPI_AWARENESS.PerMonitorAware:
                    uDpi = GetDpiForWindow(this.Handle);
                    break;
                }

                this.Font = new Font(this.Font.FontFamily, ((float)uDpi / PreviousDPI) * this.Font.Size, this.Font.Style);

                // incompleted
                foreach (var control in this.Controls)
                {
                    if (control is Label)
                    {
                        var ctrl = control as Control;
                        if (ctrl.Font != null)
                        {
                            ctrl.Font = new Font(ctrl.Font.FontFamily, ((float)uDpi / PreviousDPI) * ctrl.Font.Size, ctrl.Font.Style);
                        }
                    }
                }

                // save previous dpi
                PreviousDPI = uDpi;
                return;
            }
            base.WndProc(ref message);
        }
Exemple #6
0
 internal static System.Windows.Forms.DpiAwarenessContext TryGetDpiAwarenessContextForWindow(IntPtr hWnd)
 {
     System.Windows.Forms.DpiAwarenessContext result = System.Windows.Forms.DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNSPECIFIED;
     try
     {
         if (ApiHelper.IsApiAvailable("user32.dll", "GetWindowDpiAwarenessContext"))
         {
             if (ApiHelper.IsApiAvailable("user32.dll", "GetAwarenessFromDpiAwarenessContext"))
             {
                 IntPtr        windowDpiAwarenessContext        = GetWindowDpiAwarenessContext(hWnd);
                 DPI_AWARENESS awarenessFromDpiAwarenessContext = GetAwarenessFromDpiAwarenessContext(windowDpiAwarenessContext);
                 result = ConvertToDpiAwarenessContext(awarenessFromDpiAwarenessContext);
                 return(result);
             }
             return(result);
         }
         return(result);
     }
     catch
     {
         return(result);
     }
 }
 private static extern IntPtr _SetProcessDpiAwareness(DPI_AWARENESS awareness);
 public static IntPtr SetProcessDpiAwareness(DPI_AWARENESS awareness)
 {
     return(_SetProcessDpiAwareness(awareness));
 }
 private static extern void _GetProcessDpiAwareness(IntPtr hprocess, out DPI_AWARENESS awareness);
Exemple #10
0
 private static System.Windows.Forms.DpiAwarenessContext ConvertToDpiAwarenessContext(DPI_AWARENESS dpiAwareness)
 {
     return(dpiAwareness switch
     {
         DPI_AWARENESS.DPI_AWARENESS_UNAWARE => System.Windows.Forms.DpiAwarenessContext.DPI_AWARENESS_CONTEXT_UNAWARE,
         DPI_AWARENESS.DPI_AWARENESS_SYSTEM_AWARE => System.Windows.Forms.DpiAwarenessContext.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE,
         DPI_AWARENESS.DPI_AWARENESS_PER_MONITOR_AWARE => System.Windows.Forms.DpiAwarenessContext.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2,
         _ => System.Windows.Forms.DpiAwarenessContext.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE,
     });
Exemple #11
0
 internal static extern bool SetProcessDpiAwareness(DPI_AWARENESS awareness);