Per-Monitor支持DPI的应用程序在VS2013中以系统DPI识别的方式运行

时间:2014-07-16 06:08:02

标签: c# wpf visual-studio-2013 dpi

我发现一个WPF应用程序声明自己在应用程序清单中感知Per-Monitor DPI在Visual Studio 2013(调试和发布)中执行时运行为系统DPI。当独立时,应用程序以Per-Monitor DPI识别的方式运行。我创建了一个应用程序,它在Visual Studio 2012 Express for Windows Desktop中具有完全相同的代码,在这种情况下,它甚至在Visual Studio中也可以作为Per-Monitor DPI识别运行。

因此,我认为Visual Studio 2013中的某些内容会阻止应用程序以Per-Monitor DPI识别的方式运行。有没有人有线索或暗示?

测试应用程序背后的代码如下:

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
    }
}

,应用程序清单如下:

<?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>

1 个答案:

答案 0 :(得分:3)

正如汉斯建议禁用托管过程一样,这个问题似乎是由Visual Studio 2013的已知问题导致的,该问题忽略了debegger中的应用程序清单。

所以,我尝试了Visual Studio 2013 Update 3 RC,然后问题就消失了。此问题似乎在Update 3中得到修复。现在,应用程序清单已正确反映,并且测试应用程序在VS2013中被识别为Per-Monitor DPI。就是这样。