Reputation: 41
I'm facing a weird issue in WPF, Let's say we have a window with fixed size of 512x512 pixels, Now when we run this WPF app inside a operating system with 125% scaling DPI created window size will be 640x640 pixels which means original size is multiplied by 1.25.
I want my window be 512x512 without considering scaling value of operating system. I tried every solution provided in internet :
dpiAware
and dpiAwareness
in app.manifestSwitch.System.Windows.DoNotScaleForDpiChanges
to false in app.config[assembly: System.Windows.Media.DisableDpiAwareness]
in AssemblyInfo.csAll of the methods resulting in same invalid window size. Note that switching between DPI aware and DPI unaware mode only results in Blurry visuals and Sharp visuals inside the window, For example if I set dpi awareness to true text of the button will be clear and sharp.
Here is a minimal example project I prepared : https://github.com/CycloneRing/Issue-WPFWindowScaleDPI
Is there any way around this? I don't want to manually fix the size of my WPF windows.
Only solution I came up with is setting window size using native imports :
private void WindowLoaded(object sender, RoutedEventArgs e)
{
IntPtr hWnd = new WindowInteropHelper(this).Handle;
SetWindowPos(hWnd, IntPtr.Zero, 0, 0, (int)this.Width, (int)this.Height, 0x0002 | 0x0004 | 0x0020);
}
But this doesn't solve the main issue, One side effect of this is units inside wpf window still will be multiplied by 1.25.
Note: I'm having a theory, Since WPF windows are running inside HwndWrapper, Maybe dpi setting doesn't affect HwndWrapper and it always use the windows scaling value.
Upvotes: 0
Views: 443
Reputation: 96
I would say that it is not a smart choice to use fixed resolution in WPF apps. Most of the time, it is hard for devs to restrict the user's dpi choices as you may never know what kind of hardware your app is currently running on.
Hence, I would suggest you make your app dpi aware, and that is very doable in WPF. It would be a good idea that all of your controls and layouts' sizes depend on the actual size of your main window.
Here are some useful links: Windows Forms DPI scaling Creating a DPI-Aware Application
Upvotes: 0