ilCosmico
ilCosmico

Reputation: 1459

Font Size issue with a popup shown at design-time in a WinForms UserControl

I've a WinForms custom control with design-time features that shows a popup.

If I change the system font scaling in the Windows settings (Control Panel\All Control Panel Items\Display) then the labels in the popup are cut.

In this video you can see the different behavior with standard font size and with font size changed to 125%.

Here is screenshot of about form when opening from smart tag at design-time:

enter image description here

Here is screenshot of about form when opening at run-time:

enter image description here

How can I fix this issue?

(Source)

Upvotes: 0

Views: 564

Answers (2)

xtu
xtu

Reputation: 417

The issue in the Visual Studio (VS) designer is that Visual Studio is declared as DPI-aware which means it will handle the different DPI settings by it self. But, the About form does not seem to support different DPI as you have set its AutoScaleModel property to Font.

When About form is used in the WindowsFormsApplication1, it works "fine" because the application is declared non-DPI-aware by default. In this case, the operation system will render the form as a bitmap in a sandbox and scales it up before showing it on screen, so you can see texts are fuzzy.

In order to avoid the issue in the VS designer, you have to make sure your About form can handle different DPI settings. Actually your workaround is one of the solution to support different DPI settings.

Update: This post has detailed description of DPI awareness in WinForms application.

Upvotes: 0

ilCosmico
ilCosmico

Reputation: 1459

I found a workaround by setting the scale factor to the form before showing it.

public void About()
{
    float width, height;
    using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
    {
        width = graphics.DpiX / 96;
        height = graphics.DpiY / 96;
    }            
    About form = new About();            

    if (width != 1 || height != 1)
        form.Scale(new SizeF(width, height));

    form.ShowDialog();
}

It seems to work fine.

Upvotes: 1

Related Questions