现在1920×1080以上分辨率的高分屏电脑渐渐普及了。我们会在windows的显示设置里看到缩放比例的设置。在windows桌面客户端的开发中,有时会想要精确计算窗口的面积或位置。然而在默认情况下,无论winforms的screen.bounds.width属性还是wpf中systemparameters.primaryscreenwidth属性,以下图举例,将会返回除以150%的数值1280。而不是真实的物理分辨率1920。
接下来介绍如何获取display resolution中显示的实际分辨率。通过如下win32 api的调用:
[dllimport("gdi32.dll", entrypoint = "getdevicecaps", tlasterror = true)] 劳动补偿金 public static extern int getdevicecaps(intptr hdc, int nindex);
该方法可以获取设备的硬件信息,可以通过第二个参数nindex来指定要查询的具体信息。例如我们要用到的以像素为单位的桌面高度desktopvertres。
enum devicecap { vertres = 10, physicalwidth = 110, scalingfactorx = 114, desktopvertres = 117, // /d/file/titlepic/getdevicecaps.html }
在获得物理像素高度后,通过计算不难得出屏幕的缩放比列。
private static double getscreenscalingfactor() { var g = graphics.fromhwnd(intptr.zero); intptr desktop = g.gethdc(); var physicalscreenheight = getdevicecaps(deskto活动策划书的格式p, (int)devicecap.desktopvertres); var screenscalingfactor = (double)physicalscreenheight / screen.primaryscreen.bounds.height; //systemparameters.primaryscreenheight; return screenscalingfactor; }
在获取屏幕缩放比例后,诸如通过graphics类的copyfromscreen方法来截屏,或者精确控制窗口大小和位置才得以正确实现。
其实在winfor元素周期表ms程序中,我们还有更简单的方式来实现类似效果。即在工程中添加app.manifest文件,将<dipaware>节点的值设为true。这样修改后,screen.primaryscreen.bounds将获得实际的物理分辨率尺寸,同时你还会发现winforms程序不糊了。这是因为windows默认winforms程序不支持dpi感知,在高分屏下就直接粗暴的把窗体放大。
<application xmlns="urn:schemas-microsoft-com:asm.v3"> <windowsttings> <dpiaware xmlns="http://schemas.microsoft.com/smi/2005/windowsttings">true</年味儿作文dpiaware> </windowsttings> </application>
该设置对wpf无效,wpf默认支持dpi感知功能。而从uwp开始windows客户端技术全面支持高分屏即高dpi缩放。老旧应用程序不肯升级,以至无法支持高分屏,这锅某软背着挺冤的……
所以同学们,现在开始全面转向winui 3吧,这货是这么些年某软兜兜转转,客户端技术集大成者。用windows app sdk创建unpackged app时,恍惚间仿佛回到了在xp上装.net runtime的时光。
因为github访问时常抽风,我将示例代码在gitee上也同步了一份:
how to get windows display scale using c#. (github.com)
how to get windows display scale using c#. (gitee.com)
到此这篇关于c#获取windows10屏幕的缩放比例的文章就介绍到这了,更多相关c#获取屏幕缩放比例内容请搜索www.887551.com以前的文章或继续浏览首屈一指的意思下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 02:12:38,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/7f48b60dbf83eee04ed9db2eaa9a5665.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:C#获取Windows10屏幕缩放比例的操作方法.doc
本文 PDF 下载地址:C#获取Windows10屏幕缩放比例的操作方法.pdf
留言与评论(共有 0 条评论) |