我找到了一种方法来获得与WinAPI的dpi的。首先需要参考System.Drawing
和System.Windows.Forms
。可以从显示区域上的一点获得带有WinAPI的显示器手柄 - Screen
课程可以给我们这点。然后GetDpiForMonitor
函数返回指定监视器的dpi。
public static class ScreenExtensions
{
public static void GetDpi(this System.Windows.Forms.Screen screen, DpiType dpiType, out uint dpiX, out uint dpiY)
{
var pnt = new System.Drawing.Point(screen.Bounds.Left + 1, screen.Bounds.Top + 1);
var mon = MonitorFromPoint(pnt, 2/*MONITOR_DEFAULTTONEAREST*/);
GetDpiForMonitor(mon, dpiType, out dpiX, out dpiY);
}
//https://msdn.microsoft.com/en-us/library/windows/desktop/dd145062(v=vs.85).aspx
[DllImport("User32.dll")]
private static extern IntPtr MonitorFromPoint([In]System.Drawing.Point pt, [In]uint dwFlags);
//https://msdn.microsoft.com/en-us/library/windows/desktop/dn280510(v=vs.85).aspx
[DllImport("Shcore.dll")]
private static extern IntPtr GetDpiForMonitor([In]IntPtr hmonitor, [In]DpiType dpiType, [Out]out uint dpiX, [Out]out uint dpiY);
}
//https://msdn.microsoft.com/en-us/library/windows/desktop/dn280511(v=vs.85).aspx
public enum DpiType
{
Effective = 0,
Angular = 1,
Raw = 2,
}
有三种类型的结垢,你可以找到一个description in the MSDN。
我有一个新的WPF应用程序快速测试它:
private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
var sb = new StringBuilder();
sb.Append("Angular\n");
sb.Append(string.Join("\n", Display(DpiType.Angular)));
sb.Append("\nEffective\n");
sb.Append(string.Join("\n", Display(DpiType.Effective)));
sb.Append("\nRaw\n");
sb.Append(string.Join("\n", Display(DpiType.Raw)));
this.Content = new TextBox() { Text = sb.ToString() };
}
private IEnumerable<string> Display(DpiType type)
{
foreach (var screen in System.Windows.Forms.Screen.AllScreens)
{
uint x, y;
screen.GetDpi(type, out x, out y);
yield return screen.DeviceName + " - dpiX=" + x + ", dpiY=" + y;
}
}
我希望它能帮助!
你能在每个屏幕上创建一个虚拟窗口并以这种方式获取信息吗? – 2015-04-03 19:40:02
你可以与以前的评论,否则你将不得不枚举显示设备,并获得DPI的方式。 – Kcvin 2015-04-03 21:48:51
Windows现在支持从8.1开始的每个屏幕DPI。您可能会遇到这种情况,您需要安装一个昂贵的“视网膜”显示屏和另一个普通投影仪,通常是投影仪。 WPF背景资料[在这里](https://msdn.microsoft.com/en-us/library/windows/desktop/ee308410%28v=vs.85%29.aspx)。 – 2015-04-06 19:09:47