Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix setting DPI awareness #6245

Merged
merged 1 commit into from
May 9, 2022
Merged

Fix setting DPI awareness #6245

merged 1 commit into from
May 9, 2022

Conversation

AustinWise
Copy link
Contributor

Description

Currently DPI awareness will be enabled only if a DisableDpiAwarenessAttribute attribute is applied to the entry assembly. Which does not make a lot of sense. This also does not match the behavior of .NET 6.

This PR changes the logic to always enable DPI awareness unless a DisableDpiAwarenessAttribute attribute is applied to the entry assembly.

Customer Impact

WPF applications are not DPI aware. This can be seen by:

dotnet new wpf
dotnet run

And then checking the DPI Awareness column in Task Manager. On .NET 6 the DPI awareness is "System". In .NET 7 preview 1, it is "Unaware".

Regression

This regression was introduced by #5765, which migrated the DPI awareness enablement code from C++ to C#.

Testing

dotnet new wpf

Then replace the contents of MainWindow.xml.cs with:

using System.Windows;
using System.Runtime.InteropServices;

namespace test
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        [DllImport("User32", EntryPoint = "IsProcessDPIAware", CharSet = CharSet.Auto, SetLastError = true)]
        internal static extern bool IsProcessDPIAware();

        public MainWindow()
        {
            InitializeComponent();
            this.Title = IsProcessDPIAware().ToString();
        }
    }
}

Results of running this program on different versions of WPF:

.NET Version title
6.0.3 True
7 Preview 1 False
This PR True

Risk

Low.

@AustinWise AustinWise requested a review from a team as a code owner March 11, 2022 19:13
@ghost ghost added the PR metadata: Label to tag PRs, to facilitate with triage label Mar 11, 2022
@ghost ghost requested review from dipeshmsft, singhashish-wpf and SamBent March 11, 2022 19:13
@ghost ghost added the Community Contribution A label for all community Contributions label Mar 11, 2022
Copy link
Contributor

@ThomasGoulet73 ThomasGoulet73 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! This is definitely a mistake on my part and I believe that this is the right fix.

if (assemblyApp != null && Attribute.IsDefined(assemblyApp, typeof(System.Windows.Media.DisableDpiAwarenessAttribute)))
{
// DpiAware composition is enabled for this application.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you keep the comments at the same place as the previous C++ code? You can use this code for reference:

Assembly ^ assemblyApp;
Type ^ disableDpiAwareType = System::Windows::Media::DisableDpiAwarenessAttribute::typeid;
bool bDisableDpiAware = false;
// By default, Application is DPIAware.
assemblyApp = Assembly::GetEntryAssembly();
// Check if the Application has explicitly set DisableDpiAwareness attribute.
if (assemblyApp != nullptr && Attribute::IsDefined(assemblyApp, disableDpiAwareType))
{
bDisableDpiAware = true;
}
if (!bDisableDpiAware)
{
// DpiAware composition is enabled for this application.
SetProcessDPIAware_Internal( );
}
// Only when DisableDpiAwareness attribute is set in Application assembly,
// It will ignore the SetProcessDPIAware API call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it it to match the original C++ code more closely. I renamed bDisableDpiAware to disableDpiAware, so that it matches typical C# conventions.

Currently it is only setting DPI awareness if the entry assembly has a
DisableDpiAwarenessAttribute applied to it.
@dipeshmsft dipeshmsft self-assigned this Mar 16, 2022
@ghost ghost assigned AustinWise May 5, 2022
@dipeshmsft dipeshmsft merged commit 4bd1b45 into dotnet:main May 9, 2022
@AustinWise AustinWise deleted the austin/FixDpiAwareness branch May 9, 2022 17:47
@ghost ghost locked as resolved and limited conversation to collaborators Jun 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants