我需要从控制面板>显示设置中获得每台连接到计算机的屏幕的DPI比例,即使那些没有打开WPF窗口的屏幕也是如此。 我已经看到了一些获得DPI的方法(例如http://dzimchuk.net/post/Best-way-to-get-DPI-value-in-WPF ),但是这些似乎都依赖于Graphics.FromHwnd(IntPtr.Zero)或PresentationSource.FromVisual(visual).CompositionTarget.TransformToDevice 。
有没有办法让每个单独的屏幕DPI设置?
背景 – 我正在创build一个布局configuration编辑器,以便用户可以在启动之前设置其configuration。 为此,我绘制每个屏幕相对于彼此。 对于一种configuration,我们使用的是具有比默认DPI比例设置更大的4K显示。 它比其他屏幕的物理显示要小得多,因为它报告与其他屏幕相同的分辨率。
是否可以将版本号添加到在Windows资源pipe理器中的“属性/详细信息”中可见的文件中
如何找出为什么DLL不能在不同的程序中加载?
React-native:如何从Windows构buildios
我怎样才能让EnumWindows列出所有的窗口?
安装导轨时找不到Gem存储库
Windows上的mod_wsgi WsgiPythonPath多个path
如何在Windows中检测显示器是否为宽屏幕
重命名正在运行的程序 – 危险
C#无法启动系统EXE进程
我找到了一种方法来获得与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,}
有三种缩放类型,你可以在MSDN中找到一个描述 。
private void Window_Loaded(object sender,System.Windows.RoutedEventArgs e) { var sb = new StringBuilder(); sb.Append("Angularn"); sb.Append(string.Join("n",display(DpiType.Angular))); sb.Append("nEffectiven"); sb.Append(string.Join("n",display(DpiType.Effective))); sb.Append("nRawn"); 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; } }
我希望它有帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。