Exemplo n.º 1
0
        private void DpiAwareWindow_SourceInitialized(object sender, EventArgs e)
        {
            this.source = (HwndSource)PresentationSource.FromVisual(this);
            this.source.AddHook(this.WindowProcedureHook);

            // Determine if this application is Per Monitor DPI Aware.
            this.isPerMonitorEnabled = DpiAwareApi.GetProcessDpiAwareness() == PInvoke.PROCESS_DPI_AWARENESS.PROCESS_PER_MONITOR_DPI_AWARE;

            // Is the window in per-monitor DPI mode?
            if (this.isPerMonitorEnabled)
            {
                // It is. Calculate the DPI used by the System.
                this.SystemDpi = DpiAwareApi.SystemDPI;

                // Calculate the DPI used by WPF.
                this.WpfDpi = new Point
                {
                    X = (int)(96 * this.source.CompositionTarget.TransformToDevice.M11),
                    Y = (int)(96 * this.source.CompositionTarget.TransformToDevice.M22)
                };

                // Get the Current DPI of the monitor of the window.
                this.CurrentDpi = DpiAwareApi.GetDpiForWindow(this.source.Handle);

                // Calculate the scale factor used to modify window size, graphics and text.
                this.ScaleFactor = new Point
                {
                    X = this.CurrentDpi.X / this.WpfDpi.X,
                    Y = this.CurrentDpi.Y / this.WpfDpi.Y
                };

                // Update Width and Height based on the on the current DPI of the monitor
                this.Width  = this.Width * this.ScaleFactor.X;
                this.Height = this.Height * this.ScaleFactor.Y;

                // Update graphics and text based on the current DPI of the monitor.
                this.UpdateLayoutTransform(this.ScaleFactor);
            }
        }
Exemplo n.º 2
0
 public static Dpi GetDpi(this HwndSource hwndSource, PInvoke.MONITOR_DPI_TYPE dpiType = PInvoke.MONITOR_DPI_TYPE.MDT_DEFAULT)
 {
     return(DpiAwareApi.GetDpiForWindow(hwndSource.Handle, dpiType));
 }