How can I set the dpiAware property in a Windows application manifest to “per monitor” in Visual Studio?

限于喜欢 提交于 2019-11-26 04:49:24

问题


I need to be able to set the dpiAware property in the manifest of my application to \"per monitor\". The available choices in the properties are just to enable or disable DPI awareness. Neither of these settings works for me. I can get the behavior I want for my application if I don\'t embed the manifest in the exe, then edit the manifest manually. I want to automatically generate and embed the manifest. Is there something I am missing? (I am using Visual Studio 2013.)


回答1:


New in Windows 10 is dpiAwareness as well as dpiAware, so we need to update this example a bit. Now, it is fine because if dpiAwareness does not exist, then the settings will be inherited from dpiAware.

To enable DPI awareness in full, with the latest Win10 support (see Ref URL for other possible options), which includes 'permonitor' and 'permonitorv2', which I will use instead of 'system' because your question asks it.

<asmv3:application>
  <asmv3:windowsSettings>
    <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/pm</dpiAware> <!-- legacy -->
    <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">permonitorv2,permonitor</dpiAwareness> <!-- falls back to pm if pmv2 is not available -->
  </asmv3:windowsSettings>
</asmv3:application>

To disable, you'd do the opposite (no need for dpiAwareness since we don't support it):

<asmv3:application>
  <asmv3:windowsSettings>
    <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">unaware</dpiAware>
  </asmv3:windowsSettings>
</asmv3:application>

Then there is even 'gdiScaling' if you happen to use GDI objects to paint some of your own stuff.

<asmv3:application>
  <asmv3:windowsSettings>
    <gdiScaling xmlns="http://schemas.microsoft.com/SMI/2017/WindowsSettings">true</gdiScaling>
  </asmv3:windowsSettings>
</asmv3:application>

Reference: Microsoft on DPI Awareness as of latest Windows 10 build (also has tutorials on how to make your code DPI aware, even if it is a little tedious for larger projects)




回答2:


This manifest works, and <dpiAware>True/PM</dpiAware> is the most important part:

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



回答3:


In Windows 10 1607 a new property named dpiAwareness was introduced. It allows choosing a fallback DPI scaling options and overrides dpiAware property, if present. For the best compatibility one should specify both of these and make sure your application works with all DPI-awareness levels.

The following manifest enables per-monitor DPI-awareness version 2 on Windows 10 1607+ and system DPI-awareness on Windows 7+:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
    <assemblyIdentity type="win32" name="MyApplication" version="1.0.0.0" processorArchitecture="amd64"/>

    <asmv3:application>
        <asmv3:windowsSettings>
            <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/pm</dpiAware> <!-- fallback for Windows 7 and 8 -->
            <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness> <!-- adding v1 as fallback would result in v2 not being applied to dialogs on capable systems -->
            <gdiScaling xmlns="http://schemas.microsoft.com/SMI/2017/WindowsSettings">true</gdiScaling> <!-- enables GDI DPI scaling -->
        </asmv3:windowsSettings>
    </asmv3:application>
</assembly>

To disable DPI-awareness, you can either simply leave DPI-awareness unspecified (the default is unaware), or specify dpiAware as false.

Also note the gdiScaling property, which was added in Windows 10 1607. It enables automatic GDI scaling if set to true. It's very useful if your application uses GDI for drawing things.


References:
High-DPI scaling since Windows 10 1607
Writing DPI-aware applications
Application manifests



来源:https://stackoverflow.com/questions/23551112/how-can-i-set-the-dpiaware-property-in-a-windows-application-manifest-to-per-mo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!