winforms Windows 10操作系统上的Windows窗体图形问题

ih99xse1  于 2023-03-03  发布在  Windows
关注(0)|答案(1)|浏览(171)

当我在Windows 10中运行任何Windows窗体应用程序时,窗口内的图形看起来扭曲:

在设计时不会发生这种情况:

有人经历过吗?

(请打开图像以便更好地查看。)

2o7dmzc5

2o7dmzc51#

更新了. NET框架〉= 4.7的答案

打开app. config并添加以下部分:

<System.Windows.Forms.ApplicationConfigurationSection>
    <add key="DpiAwareness" value="PerMonitorV2" />
</System.Windows.Forms.ApplicationConfigurationSection>

有关详细信息,请参阅:High DPI support in Windows Forms.

. NET框架〈4.7

要解决此问题,您可以使用以下任一选项使应用程序支持DPI:

使用应用程序清单文件

要使应用程序可识别DPI,您可以将 * 应用程序清单文件 * 添加到项目中。然后在app.manifest文件中,取消注解与DPI相关的部分:

<application xmlns="urn:schemas-microsoft-com:asm.v3">
 <windowsSettings>
   <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
 </windowsSettings>
</application>

然后在 * app. config * 文件中添加EnableWindowsFormsHighDpiAutoResizing,并将其值设置为true:

<appSettings>
  <add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />
</appSettings>

有关详细信息,请参阅Microsoft文档中的以下主题:

  • Windows环境下高DPI桌面应用程序开发

SetProcessDPIAware API调用示例

在显示主窗体之前,你可以使用SetProcessDPIAware()方法来设置你的应用程序dpi感知并防止windows缩放应用程序。另外,你应该检查windows版本是否大于或等于vista:

static class Program
{
    [DllImport("user32.dll", SetLastError = true)]
    static extern bool SetProcessDPIAware();

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        if (Environment.OSVersion.Version.Major >= 6)
            SetProcessDPIAware();

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(true);
        Application.Run(new Form1());
    }
}
    • 说明**

1.正如上面已经提到的,建议您通过应用程序清单(而不是API调用)设置进程默认DPI感知。
1.在使用API调用之前,请阅读文档以了解支持的操作系统,以及如果DLL在初始化过程中缓存dpi设置,可能出现的争用情况。还请记住,DLL应该接受主机进程的dpi设置,而不是API调用本身。
1.您可能会发现在WinForms for. NET Core 3.0中实现的DpiHelper类非常有用。

相关问题