简体   繁体   中英

.NET WinForms high DPI Scaling - how to force forms to display correctly after display settings change?

I have a WinForms application built in .NET Core 3.1 that works fine on high DPI displays. My problem is whenever a new display is added or removed in Windows the forms just get that fuzzy look again. Restarting the application resolves this but I would like to redraw the forms appropriately whenever this happens.

I think I already found the appropriate system event to subscribe to but I just cannot make the forms redraw / repaint themselves correctly.

Subscribing to Windows event DisplaySettingsChanged like so:

using Microsoft.Win32;
(...)
SystemEvents.DisplaySettingsChanged += new EventHandler(UpdateDpiOnOpenForms);

And then adding the event handler method which I could not get to work:

public static void UpdateDpiOnOpenForms(object sender, EventArgs e)
{
    foreach (Form f in Application.OpenForms) 
    { 
        //What would the correct command be here?
    }
}

How can all open forms be forced to redraw / repaint using the new default high DPI settings just like they do after the application starts?

Use Control.Invalidate() method.

"Invalidates the specified region of the control (adds it to the control's update region, which is the area that will be repainted at the next paint operation), and causes a paint message to be sent to the control. Optionally, invalidates the child controls assigned to the control."

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.invalidate?view=net-5.0

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