Windows Forms Window Resizes When Creating a WPF Window

I have a System.Window.Forms.Form where I process each button. When I receive the first event, I create a new WPF System.Windows.Window object.

 class WPF_Window : Window { } public partial class Form1 : Form { WPF_Window wnd = null; public Form1() { InitializeComponent(); } private void Form1_MouseClick(object sender, MouseEventArgs e) { if (wnd == null) { wnd = new WPF_Window(); } } } 

On my computer, this code works as expected, but if I run it on another computer (like Windows 10), when I click on the Windows Forms window, change its size (decreasing its size).

How is this possible? How can I avoid this behavior?

+6
c # resize interop winforms wpf
source share
3 answers

You have discovered that WPF calls SetProcessDPIAware (). This happens inside the module initializer for the PresentationCore. This is formally illegal, it must always be called before the application creates any windows or loads DLLs that can cache the DPI value. Or, in other words, there are many ways that this could go wrong, you just so far discovered its moderate version. This is usually not a problem in a pure WPF application, since PresentationCore always needs to be loaded before any window is created.

They backed up to disable this behavior. Copy / paste this code, best of all, into the AssemblyInfo.cs source file for your EXE project:

  [assembly: System.Windows.Media.DisableDpiAwareness] 

But this is not so important, of course, DPI virtualization is now always enabled for all your windows. Go ahead by declaring your dpi application in your manifests so that PresentationCore can't mess it up. But with the inevitable side effect, you will find that you need to fix your Winforms UI. It can decrease only when the Form.AutoScaleMode property changes or it does something unreasonable, like hard-coding the window size. The attribute is the Q & D fix.

+5
source share

This is a method you can use. Set the width and height of the window:

 public static Graphics graphics = Graphics.FromHwnd(IntPtr.Zero); public static double GetDIPIndependent(double value, double DPI) { value = value * (graphics.DpiX / DPI); return value; } public static double GetDIPDependent(double value, double DPI) { value = value * (DPI / graphics.DpiX); return value; } public static double GetDIPIndependent(double value) { value = value * (graphics.DpiX / 96.0); return value; } public static double GetDIPDependent(double value) { value = value * (96.0 / graphics.DpiX); return value; } 

Example:

If DPI independent

 this.Width = GetDIPIndependent(this.Width) this.Height = GetDIPIndependent(this.Height) 

If DPI dependent

 this.Width = GetDIPDependent(this.Width) this.Height = GetDIPDependent(this.Height) 

I like...

+1
source share

As indicated in other answers, this is due to DPI awareness. A quick fix to disable it for those using VB.NET:

Open the app.

Enter image description here

And then uncomment this part and set the DPI value to false:

Enter image description here

Please note that this may cause your application fonts to look blurry on higher DPI devices.

+1
source share

All Articles