Edit

Compiler Warning WFO0003

Version introduced: .NET 9

Remove high DPI settings from app.manifest and configure via Application.SetHighDpiMode API or ApplicationHighDpiMode project property.

Windows Forms applications should specify application DPI-awareness via the application configuration or with the Application.SetHighDpiMode API.

Example

Your project contains an app.manifest file with the <dpiAware> setting:

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <!-- other settings omitted for brevity -->
  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
    </windowsSettings>
  </application>
</assembly>

To correct this warning

Windows Forms applications should specify application DPI-awareness via the application configuration or with the Application.SetHighDpiMode API.

Using C#

Call the ApplicationConfiguration.Initialize method before Application.Run().

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

The ApplicationConfiguration.Initialize method is generated when your app compiles, based on the settings in the app's project file. For example, look at the following <Application*> settings:

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

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

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

  </PropertyGroup>

</Project>

These settings generate the following method:

[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));
    }
}

Using Visual Basic

Visual Basic sets the High DPI mode in two places. The first is in the project properties, which affects the Visual Studio Designer, but doesn't affect the app at run-time.

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net9.0-windows</TargetFramework>
    <StartupObject>Sub Main</StartupObject>
    <UseWindowsForms>true</UseWindowsForms>
    <MyType>WindowsForms</MyType>

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

  </PropertyGroup>

</Project>

The second place is in the Visual Basic Application Framework settings. These are in the project properties page, under Application > Application Framework. These settings are saved into the My Project\Application.myapp file or in an application startup event handler.

Setting the font in Visual Basic

The run-time font, however, isn't configurable in Visual Basic through an app setting or the project file. It must be set in the ApplyApplicationDefaults event handler.

Imports Microsoft.VisualBasic.ApplicationServices

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

Manage the warning

Suppress the warning with either of the following methods:

  • Set the severity of the rule in the .editorConfig file.

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

    For more information about editor config files, see Configuration files for code analysis rules.

  • Add the following PropertyGroup to your project file:

    <PropertyGroup>
        <NoWarn>$(NoWarn);WFO0003</NoWarn>
    </PropertyGroup>
    
  • Suppress in code with the #pragma warning disable WFO0003 directive.