如何创建与Windows 7向后兼容的按监视器DPI感知的缩放和大小变化的应用程序? [英] How to create a scaling and size-changing Per-Monitor DPI-aware application that is backwards compatible with Windows 7?

查看:100
本文介绍了如何创建与Windows 7向后兼容的按监视器DPI感知的缩放和大小变化的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WPF(和DPI感知API)的新手,并且正在编写一个可以在Windows 7、8.1和10中运行的应用程序。我使用具有不同每个监视器DPI设置的多个监视器,并且对制作我的应用程序在桌面配置之间尽可能兼容。

I'm new to WPF (and to DPI-awareness APIs) and am writing an application for running in Windows 7, 8.1, and 10. I use multiple monitors with different per-monitor DPI settings, and am interested in making my application as compatible as possible across desktop configurations.

我已经知道可以向WPF应用程序添加清单,取消对DPI感知的注释并进行设置到 True / PM 。这样可以成功地使该程序在Windows 8.1和10中成为每监视器DPI感知的(因此在各种监视器上看起来清晰明了),但在Windows 7中以系统感知的方式运行。

I already know that is possible to add a Manifest to a WPF application, uncomment the section for DPI awareness, and set it to True/PM. This successfully allows the program to be Per-Monitor DPI-aware in Windows 8.1 and 10 (thus looking clean and sharp over various monitors), but run as System-aware in Windows 7.

但是,我们可以做得更好吗?
Microsoft提供了整齐的本教程介绍了如何创建一个按监视器DPI感知的WPF应用程序。
他们实际上是在用C ++创建一个新对象来替换< Window> ,该对象使用Windows 8.1 API不仅检测监视器之间的DPI更改,然后在运行时调整大小应用程序,从而有效地匹配DPI中的更改。
最终结果是,该应用程序不仅具有Per-DPI感知和清晰的外观,而且在各种尺寸的大型和小型显示器之间切换时,该应用程序还希望用户更改相同的物理尺寸(以英寸或英寸为单位)。

But can we do one step better? Microsoft provides a neat tutorial here that shows how to create a Per-Monitor DPI-Aware WPF application. What they do is actually create a new object in C++ to replace <Window> that uses Windows 8.1 APIs to not only detect the DPI change between the monitors, but then to re-size the application at runtime, effectively matching the change in DPI. The end result is that not only is the app Per-DPI aware and sharp looking, but when switching between large and small monitors of various sizes, the app looks to the user to change the same physical size (in inches or centimeters) on screen.

Microsoft的Win32代码的缺点是它与Windows 7不向后兼容。尝试在7中运行会导致应用程序崩溃。

The downside to Microsoft's Win32 code is that it is not backwards compatible with Windows 7. Trying to run in 7 results in the application crashing.

我经历了有关Dr.Dobbs的文章

I've gone through this article on Dr.Dobbs and this one by Kenny Karr but don't understand the APIs well enough to make things work.

有人知道过一种类似于微软代码的方法吗? ,在Windows 8.1及更高版本中,每个监视器都知道的应用程序在监视器之间移动时实际上会更改大小 s是否在Windows 7中是系统感知的?

Does anyone know of a way to do something similar to Microsoft's code, where in Windows 8.1 and newer, an application that is Per-Monitor aware will actually change size when moving between monitors, but runs as System-Aware in Windows 7?

推荐答案

我没有看过代码,但我的猜测是崩溃是由于win 7中缺少本机方法引起的。
解决此问题的方法是修改本机方法包装器,例如(伪代码):

I haven't looked at the code, but my guess is that the crash is caused because of missing native methods in win 7. The way to fix this is to modify the native methods wrapper, for example (pseudo code):

public int GetDPIOfMonitor(IntPtr monitorHandle)
{
   if (Enviroment.OSVersion >= "6.2") // win 8 and above
   {
      return NativeMethods.PInvoke.GetMonitorDPI(monitorHandle);
   }
   else return systemDPI;
}

这样,您不会崩溃到7岁以下,因为您没有打电话给缺少本机方法。

This way you don't crash under 7, because you are not calling the missing native methods.

这篇关于如何创建与Windows 7向后兼容的按监视器DPI感知的缩放和大小变化的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆