Get the App
SLTechnology News&Howtos  ›  Development  › 

How to get the zoom ratio of Windows10 screen in C #

Shulou Source: shulou.com Published: 2022-06-02 07:08:18 09月26日 Update

This article mainly explains "C# how to get Windows10 screen zoom", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "C# how to get Windows10 screen zoom" bar!

Now high-resolution split-screen computers with higher resolution than 1920x1080 are becoming more and more popular. We will see the zoom setting in the Windows display settings. In the development of the Windows desktop client, you sometimes want to calculate the area or location of the window precisely. By default, however, regardless of the Screen.Bounds.Width property of WinForms or the SystemParameters.PrimaryScreenWidth property of WPF, the following figure will return a value of 1280 divided by 150%. Instead of the real physical resolution of 1920.

Next, I'll show you how to get the actual resolution displayed in Display resolution. Call the following Win32 API:

[DllImport ("gdi32.dll", EntryPoint = "GetDeviceCaps", SetLastError = true)] public static extern int GetDeviceCaps (IntPtr hdc, int nIndex)

This method can obtain the hardware information of the device, and the specific information to be queried can be specified by the second parameter nIndex. For example, we want to use the desktop height DESKTOPVERTRES in pixels.

Enum DeviceCap {VERTRES = 10, PHYSICALWIDTH = 110, SCALINGFACTORX = 114,117,117,117,117,114,117,117,114,117,117,114,117,117,114,117,117,114,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,1

After obtaining the physical pixel height, it is not difficult to calculate the zoom column of the screen.

Private static double GetScreenScalingFactor () {var g = Graphics.FromHwnd (IntPtr.Zero); IntPtr desktop = g.GetHdc (); var physicalScreenHeight = GetDeviceCaps (desktop, (int) DeviceCap.DESKTOPVERTRES); var screenScalingFactor = (double) physicalScreenHeight / Screen.PrimaryScreen.Bounds.Height; / / SystemParameters.PrimaryScreenHeight; return screenScalingFactor;}

After obtaining the screen zoom ratio, such as taking a screenshot through the CopyFromScreen method of the Graphics class, or precisely controlling the size and position of the window can be achieved correctly.

In fact, in WinForms programs, we have a simpler way to achieve a similar effect. That is, add the app.manifest file to the project and set the value of the node to true. After this modification, Screen.PrimaryScreen.Bounds will get the actual physical resolution size, and you will also find that the WinForms program is not paste. This is because Windows's default WinForms program does not support DPI awareness and rudely enlarges the form directly and rudely under high-split screens.

True

This setting is not valid for WPF, and WPF supports DPI awareness by default. Starting from UWP, Windows client technology fully supports high-split screen, that is, high DPI scaling. The old application refuses to upgrade, so that it can't support the high-resolution screen.

So students, now start to fully turn to WinUI 3, this product is a soft circle for so many years, the integration of client technology. When you create a Unpackged App with Windows APP SDK, it feels like a trance back to the time when you installed .NET Runtime on XP.

Because there is a lot of trouble with Github access, I synchronized a copy of the sample code on Gitee:

How to get Windows display scale using C#. (github.com)

How to get Windows display scale using C#. (gitee.com)

Thank you for reading, the above is the content of "how to get the Windows10 screen zoom ratio of C#". After the study of this article, I believe you have a deeper understanding of how to get the Windows10 screen zoom ratio of C#. The specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Screen scale clocking resolution program high score support customer physics learning precision location information pixels content actual client properties situation technology Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information NVidia Huawei MySQL Xiaomi