我找到了一種方法來獲得與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