Windows 7中有显示设置(控制面板->显示)。它允许更改文本和屏幕上其他项目的大小。我需要获得此设置,以便能够基于设置值打开/关闭C#应用程序中的某些功能。那可能吗?
Answers:
此设置是屏幕DPI或每英寸点数。
像这样阅读:
float dpiX, dpiY;
Graphics graphics = this.CreateGraphics();
dpiX = graphics.DpiX;
dpiY = graphics.DpiY;
我认为目前X和Y值不可能不同。值96对应于100%字体缩放(较小),值120对应于125%缩放(中等),而144对应于150%缩放(较大)。但是,用户可以设置这些标准值以外的值。
请注意,除非您的应用程序声明为DPI感知的,否则您观察到的值可能会受到DPI虚拟化的影响。
this
只是任何控件。msdn.microsoft.com/en-us/library/...
在所有缩放级别上,graphics.DpiX和DeviceCap.LOGPIXELSX在Surface Pro上均返回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%
}
我认为最简单的方法是使用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感知。
这就是在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
(来源)
这是一个在Windows 10中很好用的解决方案。不需要DPI意识或其他任何东西。
public static int GetWindowsScaling()
{
return (int)(100 * Screen.PrimaryScreen.Bounds.Width / SystemParameters.PrimaryScreenWidth);
}
我在控制台应用程序中使用这种方式:
float dpiX, dpiY;
using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
{
dpiX = graphics.DpiX;
dpiY = graphics.DpiY;
}
除了125%以外,以Farshid T的答案为基础可在所有缩放系数中使用。我测试了大约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...
}
如果使用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;
}
这是一个非常老的问题,但是从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
在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>
我认为这应该为您提供所需的信息:
http://www.pinvoke.net/default.aspx/user32.getsystemmetrics
http://pinvoke.net/default.aspx/Enums.SystemMetric
编辑-哦,抱歉,现在似乎有一种更简便的方法可以不加验证地获取此信息,
http://msdn.microsoft.com/zh-CN/library/system.windows.forms.systeminformation.aspx
Ric Gaudet的答案的更完整版本:
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int GetDeviceCaps(IntPtr hDC, int nIndex);
public enum DeviceCap
{
VERTRES = 10,
DESKTOPVERTRES = 117
}
static double GetWindowsScreenScalingFactor(bool percentage = true)
{
//Create Graphics object from the current windows handle
Graphics GraphicsObject = Graphics.FromHwnd(IntPtr.Zero);
//Get Handle to the device context associated with this Graphics object
IntPtr DeviceContextHandle = GraphicsObject.GetHdc();
//Call GetDeviceCaps with the Handle to retrieve the Screen Height
int LogicalScreenHeight = GetDeviceCaps(DeviceContextHandle, (int)DeviceCap.VERTRES);
int PhysicalScreenHeight = GetDeviceCaps(DeviceContextHandle, (int)DeviceCap.DESKTOPVERTRES);
//Divide the Screen Heights to get the scaling factor and round it to two decimals
double ScreenScalingFactor = Math.Round((double)PhysicalScreenHeight / (double)LogicalScreenHeight, 2);
//If requested as percentage - convert it
if (percentage)
{
ScreenScalingFactor *= 100.0;
}
//Release the Handle and Dispose of the GraphicsObject object
GraphicsObject.ReleaseHdc(DeviceContextHandle);
GraphicsObject.Dispose();
//Return the Scaling Factor
return ScreenScalingFactor;
}