Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Forms window changes its size when I create a WPF window

I have a System.Window.Forms.Form where I handle every button click. 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 (both Windows 10) when I click the Windows Forms window changes its size (decreasing its dimensions).

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

like image 991
Nick Avatar asked Aug 27 '15 07:08

Nick


People also ask

Can you mix WPF and Windows Forms?

Yes you can, both Windows Forms within a WPF application, and WPF controls within Windows Forms.

How do I resize a Windows Form?

By dragging either the right edge, bottom edge, or the corner, you can resize the form. The second way you can resize the form while the designer is open, is through the properties pane. Select the form, then find the Properties pane in Visual Studio. Scroll down to size and expand it.

How do I fit windows form to any resolution?

simply set Autoscroll = true for ur windows form..


2 Answers

You discovered that WPF calls SetProcessDPIAware(). It happens inside the module initializer for PresentationCore. Which is formally illegal, it must always be called before an app creates any windows or load DLLs that might cache the DPI value. Or in other words, lots of ways that this can go wrong, you only discovered the mild version of it as yet. Not normally a problem in a pure WPF app since PresentationCore always needs to be loaded before any window can be created.

They did leave a back-door to disable this behavior. Copy/paste this code, best in the AssemblyInfo.cs source file for your EXE project:

   [assembly: System.Windows.Media.DisableDpiAwareness]

But that isn't much of a true fix of course, DPI virtualization is now always enabled for all of your windows. Get ahead by declaring your app dpi-aware in its manifest so that PresentationCore can't mess this up. But with the inevitable side-effect that you'll now discover that you need to fix your Winforms UI. It can only get smaller when you changed the Form.AutoScaleMode property or do something unwise like hard-coding the window size. The attribute is the Q&D fix.

like image 117
Hans Passant Avatar answered Oct 07 '22 09:10

Hans Passant


As stated in other replies that is because of the DPI awareness. A quick fix to turn it off for those using VB.NET:

Open the app.manifest

Enter image description here

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

Enter image description here

Note that this might cause your application's fonts to look more blurry on higher DPI devices.

like image 36
Carlos Borau Avatar answered Oct 07 '22 11:10

Carlos Borau