An object-oriented programming language developed by Microsoft that can be used in .NET.
Hello -
Thought I'd throw in my two cents. If you are restoring the app to a saved position that is not on the Main display, you need to call GetDpiForWindow after setting that position. Else you always get the dpi for the Main display.
For example, if I have two displays, the Main display with scaling = 150 and another display at scaling = 100 (which is the display for which the app position was saved), then GetDpiForWindow returns the dpi (144) for the Main display instead of the other display's dpi (96) if I try to get the scaling before setting the app position.
Perhaps this is obvious, but it took me quite a while to realize it as I had been trying to set the scaling from the MainWindow constructor (after InitializeComponent).
Otherwise (when you use it correctly), GetDpiForWindow works quite well.
private float GetScalingFactor()
{
// Retrieve the window handle (HWND) of the current (XAML) WinUI 3 window.
IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
if (hWnd != IntPtr.Zero)
{
// get display scaling where app window is currently sitting
return GetDpiForWindow(hWnd) / 96.0F;
}
else
{
return 1.0F;
}
}
The app manifest should support per-monitor dpAware