Author Topic: High DPI Aware Applications  (Read 3525 times)

0 Members and 1 Guest are viewing this topic.

Offline Frederick J. Harris

  • Hero Member
  • *****
  • Posts: 914
  • User-Rate: +16/-0
    • Frederick J. Harris
High DPI Aware Applications
« on: November 30, 2014, 06:22:38 PM »
In that link you provided about creating high DPI aware apps, about halfway down is this ..

Determining the DPI Scale Factor

If you define your application as a DPI-aware application, you will need to scale your application appropriately at high-DPI settings. To scale correctly, you must determine the relative DPI scale factor. The DPI scale factor uses 96 DPI as the baseline setting for determining the value. The following code example shows how to determine the DPI scale factor.

Code: [Select]
int ScaleX(int x) { _Init(); return MulDiv(x, _dpiX, 96); }
int ScaleY(int y) { _Init(); return MulDiv(y, _dpiY, 96); }

Do you know what _Init() is???  They appear to be using something they have not defined.  I guess a person is just supposed to know what and where it is.  I have not a clue.

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2481
  • User-Rate: +204/-0
Re: High DPI Aware Applications
« Reply #1 on: November 30, 2014, 10:44:37 PM »
Code: [Select]
// From CDPI::_Init()
HDC hdc = GetDC(NULL);
if (hdc)
{
    _dpiX = GetDeviceCaps(hdc, LOGPIXELSX);
    _dpiY = GetDeviceCaps(hdc, LOGPIXELSY);
    ReleaseDC(NULL, hdc);
}

In PB:

Code: [Select]
' ========================================================================================
' Retrieves the current desktop DPI scaling ratios.
' ========================================================================================
SUB AfxGetDesktopDPIRatios (BYREF rxRatio AS SINGLE, BYREF ryRatio AS SINGLE)
   LOCAL hDC AS DWORD
   hDC = GetDC(%NULL)
   rxRatio = (GetDeviceCaps(hDC, %LOGPIXELSX) / 96)
   ryRatio = (GetDeviceCaps(hDC, %LOGPIXELSY) / 96)
   ReleaseDC %NULL, hDC
END SUB
' ========================================================================================

' =====================================================================================
' Scales an horizontal coordinate according the DPI (dots per pixel) being used by the application.
' =====================================================================================
FUNCTION AfxScaleX (BYVAL cx AS SINGLE) AS SINGLE
   LOCAL hDC AS DWORD
   hDC = GetDC(%NULL)
   FUNCTION = cx * (GetDeviceCaps(hDC, %LOGPIXELSX) / 96)
   ReleaseDC %NULL, hDC
END FUNCTION
' =====================================================================================

' =====================================================================================
' Scales a vertical coordinate according the DPI (dots per pixel) being used by the application.
' =====================================================================================
FUNCTION AfxScaleY (BYVAL cy AS SINGLE) AS SINGLE
   LOCAL hDC AS DWORD
   hDC = GetDC(%NULL)
   FUNCTION = cy * (GetDeviceCaps(hDC, %LOGPIXELSY) / 96)
   ReleaseDC %NULL, hDC
END FUNCTION
' =====================================================================================

Offline Frederick J. Harris

  • Hero Member
  • *****
  • Posts: 914
  • User-Rate: +16/-0
    • Frederick J. Harris
Re: High DPI Aware Applications
« Reply #2 on: December 01, 2014, 06:41:23 PM »
Thanks Jose.  I'm looking at this today.