简体   繁体   中英

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:

在此处输入图片说明

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

在此处输入图片说明

How can I fix this issue?

( Source )

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM