简体   繁体   English

.NET WinForms 高 DPI 缩放 - 如何在显示设置更改后强制 forms 正确显示?

[英].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.我有一个内置于 .NET Core 3.1 的 WinForms 应用程序,在高 DPI 显示器上运行良好。 My problem is whenever a new display is added or removed in Windows the forms just get that fuzzy look again.我的问题是,每当在 Windows 中添加或删除新显示器时,forms 就会再次获得模糊的外观。 Restarting the application resolves this but I would like to redraw the forms appropriately whenever this happens.重新启动应用程序可以解决此问题,但我想在发生这种情况时适当地重新绘制 forms。

I think I already found the appropriate system event to subscribe to but I just cannot make the forms redraw / repaint themselves correctly.我想我已经找到了要订阅的适当系统事件,但我无法让 forms 正确重绘/重绘自己。

Subscribing to Windows event DisplaySettingsChanged like so:订阅 Windows 事件 DisplaySettingsChanged 如下:

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?如何像应用程序启动后一样,使用新的默认高 DPI 设置强制所有打开的 forms 重绘/重绘?

Use Control.Invalidate() method.使用 Control.Invalidate() 方法。

"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 https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.invalidate?view=net-5.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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