dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.28k stars 954 forks source link

`PublishAot` deployment ignores call to `Application.SetHighDpiMode` #11543

Open OwnageIsMagic opened 1 week ago

OwnageIsMagic commented 1 week ago

.NET version

.NET SDK: Version: 9.0.100-preview.5.24307.3 Commit: 35b2c21ea6 Workload version: 9.0.100-manifests.6407b7e4 MSBuild version: 17.11.0-preview-24279-02+b963c24ef

Runtime Environment: OS Name: Windows OS Version: 10.0.19045 OS Platform: Windows RID: win-x64

Microsoft.WindowsDesktop.App 9.0.0-preview.5.24306.8

Did it work in any of the earlier releases of .NET Core or .NET 5+?

No response

Issue description

dotnet publish image dotnet run or publish with <dpiAware /> in manifest image but specifying dpi awareness in manifest results in warning CSC : warning WFAC010: Remove high DPI settings from C:\Source\ttt\app.manifest and configure via Application.SetHighDpiMode API or 'ApplicationHighDpiMode' project property Display settings image

Steps to reproduce

mkdir ttt && cd ttt && dotnet new winforms Form1.cs

namespace ttt;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    var cb = new CheckBox();

    cb.Location = new Point(3, 73);
    cb.Size = new Size(152, 29);
    cb.Text = "Always on top";
    cb.UseVisualStyleBackColor = true;

    Controls.Add(cb);
    }
}

ttt.csproj

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

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

    <!-- uncomment to fix -->
    <!-- <ApplicationManifest>app.manifest</ApplicationManifest> -->

    <PublishAot>true</PublishAot>
    <_SuppressWinFormsTrimError>true</_SuppressWinFormsTrimError>

  </PropertyGroup>

</Project>

app.manifest

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">

  <assemblyIdentity type="win32" version="1.0.0.0" name="my.app"/>

  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel level="asInvoker" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>

  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
      <!-- <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2,permonitor,system</dpiAwareness> -->
    </windowsSettings>
  </application>

</assembly>
elachlan commented 1 week ago

So you followed the warning and commented out dpiAwareness from the manifest and configured the dpi settings via Application.SetHighDpiMode API or ApplicationHighDpiMode project property?

Do you have a copy of your program.cs?

OwnageIsMagic commented 1 week ago

@elachlan actually the other way around. App was looking strange in aot mode, so I tried to enable dpi awareness via manifest, it worked, but produced warning. Program.cs is default one generated by dotnet new winforms. I verified that ApplicationConfiguration.Initialize(); contains global::System.Windows.Forms.Application.SetHighDpiMode(HighDpiMode.SystemAware); All code here is the full repro.

elachlan commented 1 week ago

@Olina-Zhang can your team please verify?

OwnageIsMagic commented 1 week ago
Screen capture ![Animation](https://github.com/dotnet/winforms/assets/10534634/646cc1a2-0b6d-4fb8-b6fb-c6ffc74f2844)
Olina-Zhang commented 1 week ago

@Olina-Zhang can your team please verify?

We can repro it in the latest .NET 9.0 Preview6 SDK build with PublishAot deployment.

chrarnoldus commented 1 week ago

Is it related to this? https://github.com/dotnet/winforms/issues/9911#issuecomment-1719050890

OwnageIsMagic commented 1 week ago

@chrarnoldus dpiAware=true is equivalent to dpiAwareness=system and SystemAware is default for netcore winforms and wpf (as mentioned in comment) So using system is ok. And yes, it's probably related to initialization order.

KlausLoeffelmann commented 1 week ago

If you don't do this via the project settings, but have the code manually added in Program.cs to control HighDPI settings and other settings, does that then change anything?

Also not, that Trimming/AOTing WinForms Apps is not supported at this time, but we're working on it.

OwnageIsMagic commented 1 week ago

@KlausLoeffelmann adding this doesn't resolve the issue.

static Program()
{
    Application.SetHighDpiMode(HighDpiMode.SystemAware);
}
[System.Runtime.CompilerServices.ModuleInitializer]
internal static void M1()
{
    Application.SetHighDpiMode(HighDpiMode.SystemAware);
}

Huh, "supported" winforms trimming looks like this error NETSDK1175: Windows Forms is not supported or recommended with trimming enabled. Please go to https://aka.ms/dotnet-illink/windows-forms for more details..😉