Powerbasic Museum 2020-B

Webmaster: José Roca (PBWIN 10+/PBCC 6+) (SDK Forum) => Windows API Programming => Topic started by: Frederick J. Harris on November 30, 2014, 06:22:38 PM

Title: High DPI Aware Applications
Post by: Frederick J. Harris 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.


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.
Title: Re: High DPI Aware Applications
Post by: José Roca on November 30, 2014, 10:44:37 PM

// From CDPI::_Init()
HDC hdc = GetDC(NULL);
if (hdc)
{
    _dpiX = GetDeviceCaps(hdc, LOGPIXELSX);
    _dpiY = GetDeviceCaps(hdc, LOGPIXELSY);
    ReleaseDC(NULL, hdc);
}


In PB:


' ========================================================================================
' 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
' =====================================================================================

Title: Re: High DPI Aware Applications
Post by: Frederick J. Harris on December 01, 2014, 06:41:23 PM
Thanks Jose.  I'm looking at this today.