Home > Backend Development > C++ > How Can I Programmatically Retrieve Windows Display Scaling Settings?

How Can I Programmatically Retrieve Windows Display Scaling Settings?

Mary-Kate Olsen
Release: 2025-01-15 10:43:49
Original
743 people have browsed it

How Can I Programmatically Retrieve Windows Display Scaling Settings?

Programmatically determine Windows display settings

In Windows 7 and above, users can adjust display parameters, such as text size, in the Control Panel. Accessing these settings is useful to enable or disable application functionality based on user preference.

Can these display settings be retrieved programmatically?

It turns out that both the graphics.DpiX and DeviceCap.LOGPIXELSX properties return a consistent value of 96 on Surface Pro devices regardless of zoom level. To accurately calculate the scaling factor, another approach is required.

An effective method is to utilize the gdi32.dll function in GetDeviceCaps. By measuring the logical and physical screen height we can determine the screen scaling factor.

<code class="language-csharp">[DllImport("gdi32.dll")]
static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
public enum DeviceCap
{
    VERTRES = 10,
    DESKTOPVERTRES = 117,
}

private float getScalingFactor()
{
    using Graphics g = Graphics.FromHwnd(IntPtr.Zero);
    IntPtr desktop = g.GetHdc();
    int LogicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.VERTRES);
    int PhysicalScreenHeight = GetDeviceCaps(desktop, (int)DeviceCap.DESKTOPVERTRES); 

    g.ReleaseHdc(desktop);

    float ScreenScalingFactor = (float)PhysicalScreenHeight / (float)LogicalScreenHeight;

    return ScreenScalingFactor;
}</code>
Copy after login

By calling this function we can accurately obtain the screen scaling factor, allowing us to adjust application behavior accordingly.

The above is the detailed content of How Can I Programmatically Retrieve Windows Display Scaling Settings?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template