在Windows 7中有显示设置(控制面板 - >显示)。它允许更改屏幕上的文本和其他项目的大小。 我需要获得此设置才能够根据设置值打开/关闭C#应用程序中的某些功能。 这可能吗?如何获取Windows显示设置?
回答
此设置第e屏幕DPI或每英寸点数。
读它,像这样:
float dpiX, dpiY;
Graphics graphics = this.CreateGraphics();
dpiX = graphics.DpiX;
dpiY = graphics.DpiY;
我不认为这是可能在目前的X和Y值是不同的。值为96对应于100%字体缩放(较小),120对应于125%缩放(中等),144对应于150%缩放(较大)。但是,用户可以设置这些标准值以外的值。
请注意,除非您的应用程序被声明为支持DPI,否则您观察到的值可能会受到DPI虚拟化的限制。
我想这应该为你提供你正在寻找的信息:
http://www.pinvoke.net/default.aspx/user32.getsystemmetrics
http://pinvoke.net/default.aspx/Enums.SystemMetric
编辑 - 哦,对不起,它看起来像有一个更简单的方法,现在得到这个信息,而不一个PInvoke的,
http://msdn.microsoft.com/en-us/library/system.windows.forms.systeminformation.aspx
不幸的是,系统信息并不反映dpi比例。此外,似乎图形上下文的dpiX/dpiY属性返回96为150%(至少在Surface Pro 2上)。所以你无法区分100%和150%的变焦。有人知道一个合适的方法吗? – Zuppa 2013-12-13 13:30:03
我认为最简单的方法是使用GetDeviceCaps
函数。从pinvoke.net:
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int GetDeviceCaps(IntPtr hDC, int nIndex);
public enum DeviceCap
{
/// <summary>
/// Logical pixels inch in X
/// </summary>
LOGPIXELSX = 88,
/// <summary>
/// Logical pixels inch in Y
/// </summary>
LOGPIXELSY = 90
// Other constants may be founded on pinvoke.net
}
与用法:
Graphics g = Graphics.FromHwnd(IntPtr.Zero);
IntPtr desktop = g.GetHdc();
int Xdpi = GetDeviceCaps(desktop, (int)DeviceCap.LOGPIXELSX);
int Ydpi = GetDeviceCaps(desktop, (int)DeviceCap.LOGPIXELSY);
在这种方法中,你有没有需要标记您的应用程序为DPI意识。
不要忘记调用ReleaseHdc – BitsEvolved 2015-01-30 00:01:35
@BitsEvolved绝对。这解决了“我的”内存泄漏(在完全不同的上下文中)!非常感谢。 – Wolf 2015-04-17 09:54:38
这是你如何在WPF中做到这一点。返回值是WPF的逻辑单位,它等于1/96英寸。所以,如果你的屏幕DPI设置为96,你会得到1
Matrix m =
PresentationSource.FromVisual(Application.Current.MainWindow).CompositionTarget.TransformToDevice;
double dx = m.M11; // notice it's divided by 96 already
double dy = m.M22; // notice it's divided by 96 already
(source)
谢谢,我认为这是wpf最简单的方法。 – 2015-11-20 12:31:15
值都graphics.DpiX和DeviceCap.LOGPIXELSX在所有缩放级别上表面临返回96。
相反,我设法计算比例因子是这样的:
[DllImport("gdi32.dll")]
static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
public enum DeviceCap
{
VERTRES = 10,
DESKTOPVERTRES = 117,
// http://pinvoke.net/default.aspx/gdi32/GetDeviceCaps.html
}
private float getScalingFactor()
{
Graphics g = Graphics.FromHwnd(IntPtr.Zero);
IntPtr desktop = g.GetHdc();
int LogicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES);
int PhysicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES);
float ScreenScalingFactor = (float)PhysicalScreenHeight/(float)LogicalScreenHeight;
return ScreenScalingFactor; // 1.25 = 125%
}
哇,我已经搜索了很长时间如何获得真正的缩放因子,并且这最终解决了我的问题! – andreas 2015-01-17 01:40:11
我第二个这个。我使用的是Dell XPS 15,并且使用3200x1800的原始分辨率查找,结果比我无法计算缩放因子,以便有用地使用来自GetWindowPlacement等的值。例如,使用控制台应用程序时,缩放比例为2,使用LINQPad它是1.这是最后的答案!谢谢 – joshcomley 2015-02-07 15:54:39
那一刻,当你即将哭泣,放弃,然后你遇到这样的事情。 – 2015-04-21 22:06:25
在WPF使用下面的代码片段的情况下,
PresentationSource source = PresentationSource.FromVisual(this);
double dpiX, dpiY;
if (source != null)
{
dpiX = 96.0 * source.CompositionTarget.TransformToDevice.M11;
dpiY = 96.0 * source.CompositionTarget.TransformToDevice.M22;
}
我用这种方式在我的控制台应用程序:
float dpiX, dpiY;
using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
{
dpiX = graphics.DpiX;
dpiY = graphics.DpiY;
}
使用Farshid T的答案作为基础在每个比例因子中起作用,125%除外。我测试了大约20种不同的缩放因子,DPI总是返回96,除非设置为125%,这将返回DPI 120. 120/96 = 1.25。不知道为什么会出现这种情况,但这段代码似乎适用于任何比例设置。
[DllImport("gdi32.dll")]
static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
public enum DeviceCap
{
VERTRES = 10,
DESKTOPVERTRES = 117,
LOGPIXELSY = 90,
// http://pinvoke.net/default.aspx/gdi32/GetDeviceCaps.html
与用法:
Graphics g = Graphics.FromHwnd(IntPtr.Zero);
IntPtr desktop = g.GetHdc();
int LogicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES);
int PhysicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES);
int logpixelsy = GetDeviceCaps(desktop, (int)DeviceCap.LOGPIXELSY);
float screenScalingFactor = (float)PhysicalScreenHeight/(float)LogicalScreenHeight;
float dpiScalingFactor = (float)logpixelsy/(float)96;
if (screenScalingFactor > 1 ||
dpiScalingFactor > 1)
{
// do something nice for people who can't see very well...
}
这是一个非常古老的问题,但自从Windows 8.1,可以使用各种其他功能,如GetDpiForWindow
在C#:
[DllImport("user32.dll")]
static extern int GetDpiForWindow(IntPtr hWnd);
public float GetDisplayScaleFactor(IntPtr windowHandle)
{
try
{
return GetDpiForWindow(windowHandle)/96f;
}
catch
{
// Or fallback to GDI solutions above
return 1;
}
}
为了在Windows 10周年中正常工作,您需要为您添加一个app.manifest
r C#项目:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1"
manifestVersion="1.0">
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<!-- The combination of below two tags have the following effect :
1) Per-Monitor for >= Windows 10 Anniversary Update
2) System < Windows 10 Anniversary Update -->
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitor</dpiAwareness>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">True/PM</dpiAware>
</windowsSettings>
</application>
</assembly>
- 1. 获取系统显示颜色设置
- 2. Haxe flash API - 如何获取/设置闪存显示大小?
- 3. 如何在iPhone设置显示中获取不可编辑的文本显示
- 4. 如何获取Windows区域位置设置?
- 5. 如何获取/设置rootViewController?
- 6. SWT问题与显示设置的Windows
- 7. Python tkinter - Windows更大的显示设置
- 8. 如何设置datepicker的显示位置?
- 9. 获取Windows的区域设置格式
- 10. 在Windows上获取OpenGL设置(mingw32?)
- 11. 如何获取并设置Windows资源管理器视图设置
- 12. 如何获取Windows XP上连接的显示器的类型?
- 13. C#如何获取Windows显示器名称
- 14. 如何从当前显示语言获取Windows文本资源
- 15. 如何获取语言和输入设置下显示的语言列表
- 16. 用户语言设置 - 获取设置和显示的最有效方式
- 17. 如何使用WinAPI/C获取显示器/显示设备名称#
- 18. 如何为Ubuntu设置显示变量
- 19. 如何显示设置为RAW
- 20. 如何设置大显示器的列?
- 21. 如何设置显示QToolBar的行?
- 22. 如何设置表单显示
- 23. datePickerDialog如何设置显示日期?
- 24. 如何隐藏/显示iPhone设置?
- 25. 如何设置MKPlacemark的显示名称?
- 26. 如何获得无障碍显示设置
- 27. 如何可靠捕获显示设置改变
- 28. jQuery的:如何获得与设置为显示无
- 29. 获取设备位置时不显示蓝点单击
- 30. 无法获取设置按钮来显示动态壁纸
您对哪些设置感兴趣?有许多。 – 2011-05-12 11:46:11
如果您使用设置 - 控制面板 - >显示打开此页面。只有一个设置可以改变屏幕上文本和其他项目的大小。选项为“较小”,“中等”和“较大” – newmember 2011-05-12 11:50:23