Requires: Direct2D.lg32
Adjusts the size of a Form based on the specified client width and height in DIPs.
D2AdjustW frm, width!, height!
frm | : Form object |
width!, height! | : float expression |
The D2AdjustW command adjusts the window size to the specified client size expressed in DIPs. One DIP is 1/96 inch, which is 1 pixel on a regular 96 dots-per-inch display. D2AdjustW takes the DPI for the window (the DPI of the screen the window is running) and calculates the window size after applying the DPI scaling to the specified DIP values. To have a window scaled properly on all displays the D2AdjustW is used when the application is started and when the DPI is changed. This applies only to dpi-aware programs, which may like to specify the client area in DIPs rather than in pixels. By adjusting the window size the application makes sure the client size on a high-resolution display is equally sized as it is on a regular 96 dots-per-inch display.
'
' D2AdjustW sample
' Shows how to handle a dpi-aware app used
' on a multiple monitor system with a high-resolution display.
'
$Library "Direct2D"
$Library "winuser.inc" ' Defines WM_DPICHANGED
OpenW 1, 0, 0, 0, 0, ~15 ' Create a window
Dim m$() : Array m$() = "File" #10 "Dummy" #10
Menu m$() ' Add a dummy menu
' Set the clientarea to 300x200 DIP
Global Float Width1 = 300, Height1 = 200
D2AdjustW Win_1, Width1, Height1
Global Object RT1
Set RT1 = D2GetRT( , Me.hDC)
' D2GetRT automatically detects and uses the DPI for the window.
' Reason: the render target's DPI must match the display's DPI,
' otherwise the GetSize function returns wrong values.
' For the same reason the DPI of the rendertarget must be
' adjusted explicitly when the DPI changes.
Do
Sleep
Until Me Is Nothing
Sub Win_1_Paint
Local Float w, h
D2BeginDraw RT1, D2C_Aquamarine
' Note - For a dpi-unaware program the size in
' DIPs and pixels are equal (96).
D2GetSizeRT RT1, w, h
D2Text 0, 0, "Width in DIPs: " + Str(w)
D2Text 0, 16, "Height in DIPs: " + Str(h)
D2GetSizeRT RT1, w, h, True
D2Text 0, 32, "Width in pixels: " + Str(w)
D2Text 0, 48, "Height in pixels: " + Str(h)
D2EndDraw
EndSub
Sub Win_1_MessageProc(hWnd%, Mess%, wParam%, lParam%, retval%, ValidRet?)
' WM_DPICHANGED is sent in dpi-aware apps after a DPI change
' or a window movement to another display with another DPI.
' The sizes of window and rendertarget must be changed accordingly.
' You could also the suggested size in lParam.
If Mess% = WM_DPICHANGED
' Adjust window based on the required client size in DIPs
' and current DPI.
' To get hold of the clientsize, you could use D2GetSize as well.
D2AdjustW Win_1, Width1, Height1
' Adjust the rendertarget for DPI and size.
D2SetDpiRT RT1, LoWord(wParam%), HiWord(wParam%) ' set new DPI
D2ResizeRT RT1, _X, _Y ' size from adjusted window
retval% = 0, ValidRet? = True
EndIf
EndSub
Sub Win_1_ReSize
D2ResizeRT RT1, _X, _Y
EndSub
D2AdjustW may also be used in a dpi-unaware program as a (better) replacement of Form.Adjust or AdjustW. These GB commands calculate the size of window based on the client size, but do not take a into account the presence of a window menu as D2AdjustW does. The heart of the D2AdjustW command is a call to the new API AdjustWindowRectExForDpi(). When used with a dpi-unaware program the DPI is 96 and the result is the same as with AdjustWindowRect().
As with GB's commands D2AdjustW does not take into account any toolbar and/or statusbar.
For more information on using multiple monitors with a dpi-aware GFA-BASIC program see D2SetDpiRT.
D2SetDpiRT, D2GetSizeRT, D2GetRT
{Created by Sjouke Hamstra; Last updated: 13/02/2021 by James Gaite; Other Contributors: Jean-Marie Melanson}