編譯程式警告WFAC010

引入版本: .NET 6

從 app.manifest 移除高 DPI 設定,並透過 Application.SetHighDpiMode API 或 ApplicationHighDpiMode 項目屬性進行設定。

Windows Forms 應用程式應透過應用程式設定或使用 Application.SetHighDpiMode API 來指定應用程式 DPI 感知。

這很重要

從 .NET 9 開始,此警告已變更為 WFO0003

因應措施

使用 C#

使用新的引導程序 API,需在 之前呼叫 ApplicationConfiguration.Initialize 方法。

class Program
{
    [STAThread]
    static void Main()
    {
        ApplicationConfiguration.Initialize();
        Application.Run(new Form1());
    }
}

應用程式編譯時會根據應用程式專案檔中的設定來產生 ApplicationConfiguration.Initialize 方法。 例如,請查看下列 <Application*> 設定:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
    <ImplicitUsings>enable</ImplicitUsings>

    <ApplicationVisualStyles>true</ApplicationVisualStyles>
    <ApplicationUseCompatibleTextRendering>false</ApplicationUseCompatibleTextRendering>
    <ApplicationHighDpiMode>SystemAware</ApplicationHighDpiMode>
    <ApplicationDefaultFont>Microsoft Sans Serif, 8.25pt</ApplicationDefaultFont>

  </PropertyGroup>

</Project>

這些設定會產生下列方法:

[CompilerGenerated]
internal static partial class ApplicationConfiguration
{
    public static void Initialize()
    {
        global::System.Windows.Forms.Application.EnableVisualStyles();
        global::System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
        global::System.Windows.Forms.Application.SetHighDpiMode(HighDpiMode.SystemAware);
        global::System.Windows.Forms.Application.SetDefaultFont(new Font(new FontFamily("Microsoft Sans Serif"), 8.25f, (FontStyle)0, (GraphicsUnit)3));
    }
}

使用 Visual Basic

Visual Basic 目前的運作方式與 C# 略有不同。 Visual Studio 需要專案檔設定才能偵測應用程式設定,但您還必須在專案的 [應用程式]>[應用程式架構] 屬性頁 (這會影響 My Project\Application.myapp 檔案) 或在應用程式啟動事件中設定這些設定。

這很重要

專案屬性中無法設定字型。

下列程式碼範例示範如何處理 ApplyApplicationDefaults 事件,以設定預設字型和 HighDPI 模式:

Imports Microsoft.VisualBasic.ApplicationServices

Namespace My
    Partial Friend Class MyApplication
        Private Sub MyApplication_ApplyApplicationDefaults(sender As Object, e As ApplyApplicationDefaultsEventArgs) Handles Me.ApplyApplicationDefaults
            e.Font = New Font("Microsoft Sans Serif", 8.25)
            e.HighDpiMode = HighDpiMode.SystemAware
        End Sub
    End Class
End Namespace

管理警示

使用下列其中一種方法隱藏警告:

  • .editorConfig 檔案中設定規則的嚴重性。

    [*.{cs,vb}]
    dotnet_diagnostic.WFAC010.severity = none
    

    如需編輯器配置檔的詳細資訊,請參閱 程式代碼分析規則的組態檔。

  • 將下列 PropertyGroup 新增至項目檔:

    <PropertyGroup>
        <NoWarn>$(NoWarn);WFAC010</NoWarn>
    </PropertyGroup>
    
  • 在程式碼中使用 #pragma warning disable WFAC010 指示詞來抑制。