Does Aero manage DPI and screen size? What for?

In my application, I am getting the screen resolution with this code:

SystemInformation.PrimaryMonitorSize

      

And I get DPI with this code:

    using (Graphics g = Graphics.FromHdc(NativeMethods.GetDC(IntPtr.Zero)))
    {
        dpiX = g.DpiX;
        dpiY = g.DpiY; 
    }

      

This works great in most situations. But when the code is run on a Vista machine with Aero enabled and the user has determined that the monitor is using a high DPI, unexpected results come in.

Suppose the user has set the machine to be running at 1024 x 768 and has a DPI of 144, the above code will return 683 x 512 and DPI 96.

However, if Aero is turned off, the results are returned as I would expect them to be. What does Aero do and how can I get true resolution and DPI?

+2


source to share


2 answers


You need to declare your application as DPI capable , either in the manifest (preferred) or using the SetProcessDPIAware

Win32 API function . Since so many applications are written incorrectly, Vista added "DPI virtualization" which allows applications to pretend they are running on a "standard" screen 96 dpi and then scales itself - DWM, being a layout window manager, can scale. If your application handles DPI correctly, you must state it explicitly.



On a side note, you shouldn't be guessing with HWND_DESKTOP

(which is (HWND)0

). When compositing ("Aero") is off, it actually represents the physical screen, so if you get your DC and draw it, you draw directly in the framebuffer. By definition, this is not possible with the layout window manager . I'm not sure if this affects the DPI measurement, but it might be better to use a DC for your application window, one way or another - it won't hurt /

+7


source


These links also contain useful information:

http://msdn.microsoft.com/en-us/library/dd464660(VS.85).aspx



http://blogs.msdn.com/greg_schechter/archive/2006/08/07/690704.aspx

0


source