Get scale of screen

好久不见. 提交于 2019-12-19 09:18:02

问题


We can get resolution and stuff for our screen is Screen class. So, we should use Screen.WorkingArea.Width and Screen.WorkingArea.Height, if we want to place something in center of screen.

But, in Windows 8.1 (not sure about other OSs) there is possibility to scale items on screen, if they seem too small. You can check it, by calling context menu on desktop, select "Screen Resolution", and then select "Make text and other items larger or smaller".

And the problem is, that, it seems, it does actually increase screen resolution! So, Screen.WorkingArea.Width and Screen.WorkingArea.Height will give you scaled value, and point Screen.WorkingArea.Width/2 Screen.WorkingArea.Height/2 will no longer in center of actual screen (for 200% scaling this point will be on lower right corner of screen). That might screw a lot of placement of UI items.

So, is it possible to get value of scaling? I can't find any class containing this info.


回答1:


Most methods to retrieve DPI depends on existing controls, which may be inconvenient sometimes. But DPI can be always retrieved from registry. In C#:

using Microsoft.Win32;
//...
var currentDPI = (int)Registry.GetValue("HKEY_CURRENT_USER\\Control Panel\\Desktop", "LogPixels", 96);

Scale will be

var scale = 96/(float)currentDPI;


来源:https://stackoverflow.com/questions/32607468/get-scale-of-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!