WPF - Add NotifyDpiChange method

Extract DPI change code into a public method that can be called when the `WPF` window changes it's DPI
As we target .Net 4.5.2 we don't have access to the new managed DPI change notification that was added in WPF (.Net 4.6)

Resolves #2856
This commit is contained in:
amaitland
2019-08-23 12:28:44 +10:00
parent 61e82bc50c
commit 07ac50064b
+36 -24
View File
@@ -1674,33 +1674,11 @@ namespace CefSharp.Wpf
{
source = (HwndSource)args.NewSource;
var matrix = source.CompositionTarget.TransformToDevice;
var notifyDpiChanged = DpiScaleFactor > 0 && !DpiScaleFactor.Equals(matrix.M11);
DpiScaleFactor = (float)source.CompositionTarget.TransformToDevice.M11;
WpfKeyboardHandler.Setup(source);
if (notifyDpiChanged && browser != null)
{
browser.GetHost().NotifyScreenInfoChanged();
}
var matrix = source.CompositionTarget.TransformToDevice;
//Ignore this for custom bitmap factories
if (RenderHandler is WritableBitmapRenderHandler || RenderHandler is InteropBitmapRenderHandler)
{
if (DpiScaleFactor > 1.0 && !(RenderHandler is WritableBitmapRenderHandler))
{
const int DefaultDpi = 96;
var scale = DefaultDpi * DpiScaleFactor;
RenderHandler = new WritableBitmapRenderHandler(scale, scale);
}
else if (DpiScaleFactor == 1.0 && !(RenderHandler is InteropBitmapRenderHandler))
{
RenderHandler = new InteropBitmapRenderHandler();
}
}
NotifyDpiChange(matrix.M11);
var window = source.RootVisual as Window;
if (window != null)
@@ -2323,6 +2301,40 @@ namespace CefSharp.Wpf
});
}
/// <summary>
/// Manually notify the browser the DPI of the parent window has changed.
/// </summary>
/// <param name="newDpi">new DPI</param>
/// <remarks>.Net 4.6.2 adds HwndSource.DpiChanged which could be used to automatically
/// handle DPI change, unforunately we still target .Net 4.5.2</remarks>
public void NotifyDpiChange(double newDpi)
{
var notifyDpiChanged = DpiScaleFactor > 0 && !DpiScaleFactor.Equals(newDpi);
DpiScaleFactor = (float)newDpi;
if (notifyDpiChanged && browser != null)
{
browser.GetHost().NotifyScreenInfoChanged();
}
//Ignore this for custom bitmap factories
if (RenderHandler is WritableBitmapRenderHandler || RenderHandler is InteropBitmapRenderHandler)
{
if (DpiScaleFactor > 1.0 && !(RenderHandler is WritableBitmapRenderHandler))
{
const int DefaultDpi = 96;
var scale = DefaultDpi * DpiScaleFactor;
RenderHandler = new WritableBitmapRenderHandler(scale, scale);
}
else if (DpiScaleFactor == 1.0 && !(RenderHandler is InteropBitmapRenderHandler))
{
RenderHandler = new InteropBitmapRenderHandler();
}
}
}
/// <summary>
/// Legacy keyboard handler uses WindowProc callback interceptor to forward keypress events
/// the the browser. Use this method to revert to the previous keyboard handling behaviour