.netwinforms.net-6.0.net-8.0.net-4.8

Problem with form sizes (scaling) after migrating WinForms application from .NET Framework to .NET 6 or .NET 8


We are in the process of migrating a fairly large WinForms code base from .NET Framework 4.8 to .NET 6 (or .NET 8, same issue there).

Most things seem to work. However, there is one visually obvious problem I struggled to find a solution for.

The problem is that the sizes of forms are not correct at runtime. Every form is a little bit larger at runtime than at design time.

I managed to track the problem down to the AutoScaleDimensions property.

Inside the generated designer code, there is a call to ResumeLayout(). This call seems to rescale the form size according to the difference between the two values of AutoScaleDimensions (so by a factor of 7/6 = 1.1666 for the width, and 15/13 = 1.1538 for the height).

Below you find the values of certain properties when stepping through the designer code:

private void InitializeComponent()
{
    components = new System.ComponentModel.Container();
    this.SuspendLayout();
    // Here, Size = 300x300, AutoScaleDimensions = 0x0
    this.AutoScaleDimensions = new System.Drawing.SizeF(6.0!, 13.0!); 
    // Now, Size = 300x300, AutoScaleDimensions = 6x13
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    // Now, Size = 300x300, AutoScaleDimensions = 6x13
    this.ClientSize = new System.Drawing.Size(500, 500);
    // Now, Size = 516x539, ClientSize = 500x500, AutoScaleDimensions = 6x13
    this.ResumeLayout(false);
    // Now, Size = 599x616, ClientSize = 583x577, AutoScaleDimensions = 7x15
}

When I remove the SuspendLayout/ResumeLayout calls, it works as expected (the ClientSize stays at 500x500). However, this might have other unintended consequences. I assume there is a reason why the designer put them there.

I looked into the disassembly of the CurrentAutoScaleDimensions property and noticed that it depends on the font size (due to AutoScaleMode.Font).

It appears that the default font was changed in .NET 6 to be Segoe UI at size 9pt. And that's where the AutoScaleDimensions gets its value from. The solution appears to be to change the default font for the application back to the old one, like so:

System.Windows.Forms.Application.SetDefaultFont(new Font(new FontFamily("Microsoft Sans Serif"), 8.25F))

Solution

  • The solution is to change the default font for the application back to the old one, by adding the following line to your startup method:

    System.Windows.Forms.Application.SetDefaultFont(new Font(new FontFamily("Microsoft Sans Serif"), 8.25F))
    

    The important bit is the font size. The font can be Segoe UI if you prefer, it will still work.