-2

How to get the screen resolution? The fact is that if you change the zoom option in the settings of the OS method Screen.AllScreens[screenIndex].Bounds.Width gives the wrong resolution. The problem occurs in the 4k displays. Help to solve. Thank you very much in advance!

private static Rectangle GetScreenRect(int screenIndex)
    {
        Rectangle screenRect = new Rectangle();
        screenRect.Size = new Size((Screen.AllScreens[screenIndex].Bounds.Width, Screen.AllScreens[screenIndex].Bounds.Height);
        screenRect.Location = new Point(Screen.AllScreens[screenIndex].Bounds.X, Screen.AllScreens[screenIndex].Bounds.Y);
        return screenRect;
    }

P.S: I use .NET 2.0 only.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
GPPSoft
  • 480
  • 1
  • 6
  • 15

1 Answers1

1

That's not the wrong result :)

It's a part of the compatibility layer to allow old applications to run relatively well on systems with high DPI. Your application isn't telling Windows it's new and cool enough to handle it well, so Windows pretends that its screens are smaller than they really are - this allows your application to render as usual, while Windows handles making your application big enough to comfortably be used.

The truth is, while Winforms was actually designed to be a nice framework to allow you to handle multiple DPIs (and localisation etc.), it wasn't actually used that way. So most applications are just as dumb as, say, old-school MFC applications - they just don't scale well.

In fact, your application is exactly the reason why those compatibility layers exist - you're still using .NET 2.0, long after it has been deprecated (and I assume you still support Windows XP as well). If Windows didn't lie to your application, it would be absolutely unusable, with texts to tiny to read (or controls randomly thrown around as some autoscale and others don't).

Now, if you feel that your application is actually able to handle DPI scaling properly (if you didn't design it to do that, it probably doesn't), you can use the SetProcessDpiAwareness function to tell Windows that you don't need those sweet lies - you can handle the scaling yourself, thank you. This has many benefits (everything will look much sharper), but it means you'll need to fix your scaling everywhere. Note that this function doesn't exist on Windows pre-6 at all (which doesn't do DPI correction), and you need to use SetProcessDPIAware on Windows 6+ (SetProcessDpiAwareness is Windows 8.1+).

Luaan
  • 62,244
  • 7
  • 97
  • 116