简体   繁体   中英

Per-Monitor DPI aware app runs as System DPI aware in VS2013

I found a WPF app which declares itself Per-Monitor DPI aware in the application manifest runs as System DPI aware when it is executed in Visual Studio 2013 (both debug and release). The app runs as Per-Monitor DPI aware when stand alone. I created an app which has exactly the same code in Visual Studio 2012 Express for Windows Desktop and in that case, it runs as Per-Monitor DPI aware even in Visual Studio.

So I think something in Visual Studio 2013 prevents the app from running as Per-Monitor DPI aware. Does anyone has a clue or suggestion?

The code behind of test app is as follows:

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

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);

        var awareness = GetDpiAwareness();
        if (awareness.HasValue)
            this.Title = awareness.Value.ToString();
    }

    private PROCESS_DPI_AWARENESS? GetDpiAwareness()
    {
        var ver = Environment.OSVersion.Version;
        if (!((6 == ver.Major && 3 <= ver.Minor) || (7 <= ver.Major)))
            return null;

        PROCESS_DPI_AWARENESS value;
        var result = GetProcessDpiAwareness(IntPtr.Zero, out value);
        if (result != 0)
            return null;

        return value;
    }

    [DllImport("Shcore.dll")]
    private static extern int GetProcessDpiAwareness(IntPtr hprocess, out PROCESS_DPI_AWARENESS value);

    private enum PROCESS_DPI_AWARENESS
    {
        Process_DPI_Unaware = 0,
        Process_System_DPI_Aware = 1,
        Process_Per_Monitor_DPI_Aware = 2
    }
}

and the application manifest is as follows:

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.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>

  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
    </application>
  </compatibility>

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

</asmv1:assembly>

As Hans suggested on disabling the Hosting process, this problem seem to be resulted from known issue of Visual Studio 2013 which ignores the application manifest in the debegger.

So, I tried Visual Studio 2013 Update 3 RC and then the problem has gone. This issue seems to be fixed in Update 3. Now the application manifest is properly reflected and the test app is recognized as Per-Monitor DPI in VS2013. That's it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM