WPF DPI值Per Monitor

时间:2017-05-03 20:51:03

标签: c# wpf dpi

是否可以获取特定显示器的DPI值?

我已经尝试了所有在线方法,但它们总是返回主监视器的值。

我用过的例子:

示例1

using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
{
       dpiX = g.DpiX;
       dpiY = g.DpiY;
}

示例2

PresentationSource source = PresentationSource.FromVisual(this);

double dpiX, dpiY;
if (source != null) {
    dpiX = 96.0 * source.CompositionTarget.TransformToDevice.M11;
    dpiY = 96.0 * source.CompositionTarget.TransformToDevice.M22;
}

示例3

var dpiXProperty = typeof(SystemParameters).GetProperty("DpiX", BindingFlags.NonPublic | BindingFlags.Static);
var dpiYProperty = typeof(SystemParameters).GetProperty("Dpi", BindingFlags.NonPublic | BindingFlags.Static);

var dpiX = (int)dpiXProperty.GetValue(null, null);
var dpiY = (int)dpiYProperty.GetValue(null, null);

以上都不适用于特定的监视器

2 个答案:

答案 0 :(得分:1)

您可以使用:

GetDpiForMonitor

现在,这取决于你是否已经说过你的应用程序是否具有DPI意识,关于如何获得每个监视器的dpi值 - 如果你不知道DPI,那么你可能会对实际的DPI撒谎

如果你这样做:

SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE).

(或在app.manfest中设置)。

接下来你可以做的是枚举监视器,并调用GetDpiForMonitor来获取用户为每个监视器设置的实际DPI。

您可以使用此代码使用System.Windows.Forms.ScreenMonitorFromPoint来获取每个屏幕的句柄。

如果你看一下:

那里有一个示例WPF应用程序,带有一些帮助程序调用(在托管c ++中)...但是您可以同样执行自己的DLL导入WIN32 API等。

答案 1 :(得分:0)

你需要.Net 4.7

请参阅:https://msdn.microsoft.com/en-us/library/ms171868(v=vs.110).aspx

章节:

  • 高DPI支持
  • 每监视DPI