Search code examples
delphiwindows-7screen-resolution

Getting custom DPI percentage in Delphi


Trying to my Delphi 2010 application more user freindly in high DPI modes in Windows 7 I have been trying several methods to retrive PixelsPerInch and compare to 96. Alas, no matter what I tried I always get 96. My questions are:

  1. What is the best practice to get custom DPI mode?
  2. Is the fact I am getting a constant 96 no matter what I what the percebtage is means I missing somthing?

Here is what I had tried

dpiX := Form1.PixelsPerInch

and

dpiX := Screen.PixelsPerInch

and finally:

D2DFactoryOptions.DebugLevel := D2D1_DEBUG_LEVEL_NONE;
pD2DFactoryOptions := @D2DFactoryOptions;
if D2D1CreateFactory(
    D2D1_FACTORY_TYPE_SINGLE_THREADED,
    IID_ID2D1Factory,
    PD2DFactoryOptions,
    D2DFactory
    ) <> S_OK then exit;
D2DFactory.GetDesktopDpi(dpiX, dpiY)

Care to guess? that's right dpiX is a constant 96 in 100%, 125% and 150%

Please advice.


Solution

  • I think you need to mark your application as being high DPI aware by including this in your application manifest:

    <asmv3:application xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
      <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
        <dpiAware>true</dpiAware>
      </asmv3:windowsSettings>
    </asmv3:application>
    

    Details on declaring DPI awareness are given here.

    It seems like you are currently falling back to what is called DPI Virtualization.