Requires: GfaWinx.lg32
Returns the dots per inch (dpi) value of the display for the specified window or form.
dpi% = WinDpi(frm | hWind)
frm | : Form object |
hWnd | : Window handle |
WinDpi is a wrapper for GetDpiForWindow() API function and requires Windows 8.1. When the argument is omitted WinDpi will return the DPI for Me. Otherwise, WinDpi takes either a Form object or a window handle (bit faster) and returns the DPI of the display for that window; alternatively, if the first parameter is set to 0, WinDpi returns the system DPI (the SysDpi constant can be used instead, e.g. systemdpi% = WinDpi(SysDpi).
If the OS doesn't support GetDpiForWindow the function returns the DPI for the mainscreen. In a system with multiple display monitors, this value is the same for all monitors. Windows version below 8.1 do not properly support a dpi-aware program with multiple monitors with different resolutions.
See the ScaleToDpi topic for more information on developing dpi-aware programs.
$Library "gfawinx"
Global context% = DpiAwareness()
' Open window on the display of choice
OpenW 1, -660, 0, 320, 260
MsgBox "Win 1 display's dpi is" & WinDpi(Me)
Do
Sleep
Until Me Is Nothing
DpiAwareness(context%)
Use the ScaleToDpi function to scale values based on a 96 dots per inch coordinate system to the current DPI of the screen (where the window is displayed). For performance reasons the scaling can be inlined using GB's Scale function.
Local Long dpi = WinDpi(Me) ' dpi for Me
value = Scale(pixels, dpi, 96)
' Here: all other scalings that use the same dpi
The application could also use GetDpiForWindow() API directly, which is declared in winuser.inc.lg32.
{Created by James Gaite; Last updated: 07/03/2022 by James Gaite}