Requires: Direct2D.lg32
Creates and activates a render target for a window or a device context to allow Direct2D drawing.
Set RT = D2GetRT([form | hWnd][, width%, height%],[ alphaMode%])
RT | : Object variable |
form | : Form expression |
hWnd | : Handle expression |
width%, height%, alphaMode% | : integer expression |
By default, without any arguments, D2GetRT creates a render target for a device context using the device context from Me and _X and _Y for the render target's size - NOTE: when referencing an Ocx Form child window, the form parameter must be included as it can not always be relied upon that Me will refer to the Ocx Form child window.
When the first parameter specifies a window-handle D2GetRT creates a window-render target. The width% and height% specify the size of the render target. For a window render target width% and height% are mapped to the actual size of the clientarea, scaling the coordinates of the drawings if necessary.
To copy the behavior of GB32’s ScaleWidth and ScaleHeight, the application creates a window-rendertarget and sets the width% and height% parameters to the ScaleWidth and ScaleHeight values. The offset values of ScaleLeft and ScaleTop are set using D2Transform.
After the render target is created it is activated immediately and all subsequent D2* commands are applied to this render target.
D2GetRT creates and returns a COM object that must be stored in a global Object variable using the Set command. You cannot use the dot-operator to access properties and methods on the object. The Object variable only holds a reference to the object which is used in the Direct2D library to call the COM-object methods using StdCall(). A great advantage of storing the COM object in an Object data type is the automatic releasing of the COM objects when they go out of scope. The render target can be released explicitly by setting the COM object to Nothing. The application should destroy the device dependent resources (brushes and bitmaps) as well.
An application should create render targets once and hold on to them for the life of the application. When D2GetRT is called using defaults make sure Me is the form for the new render target. Usually, a render target and its resources are created in a separate procedure that can also be invoked when D2EndDraw returns with an error. See D2EndDraw for more information.
When D2GetRT creates a a render target for a window it eventually invalidates the window to generate a WM_PAINT message. The event sub Paint is executed when entering the main message loop.
Resizing a window will adjust the scale of the window render target. A device context render target from a resizable window needs to be resized in the Resize event sub, it does not automatically adjusts the scale as a window render target does.
The render target provides the tools to draw on the surface. The render target must be activated before any of its methods can be used. After creating the render target with D2GetRT() it is activated by a silent call to D2SetRT(). After obtaining the render target it can be used to create device dependent resources like brushes and bitmaps using D2* functions like D2Brush(), D2BrushGradient(), D2Bitmap(), etc. Device dependent resources are stored, if possible, on the video-card device, hence the term device dependent.
The alphaMode% parameter can have the following values, depending on the type of the rendertarget:
'
' D2GetRT sample (dpi-unaware)
'
$Library "direct2d"
$Library "gfawinx"
Global Object Win1RT, frmRT
OpenW 1, 0, 0, 320, 260, ~15
Set Win1RT = D2GetRT(Me.hWnd, 640, 480) ' window
' Here: create Win_1 render target resources
OpenW 2, 0, 270, 320, 260, ~15
' Create and use an RT for an Ocx Form:
Ocx Form frm = "", 0, 0, _X, _Y
Local Int width, height
GetClientSize frm.hWnd, width, height
Set frmRT = D2GetRT(frm, width, height)
' Here: create frm render target resources
Do
Sleep
Until Me Is Nothing
Sub Win_1_Paint
D2BeginDraw Win1RT
D2Clear D2C_Beige
D2Box 40, 40, 600, 440
D2EndDraw
EndSub
Sub frm_Paint
Local Int width, height
D2BeginDraw frmRT
D2DefFill D2C_Coral
GetClientSize frm.hWnd, width, height
D2PBox 10, 10, width - 10, height - 10
D2EndDraw
EndSub
The first time D2GetRT is called it will load the necessary DLLs and create the factory objects.
The render target uses the GPU to accelerate rendering operations and to create drawing resources; these are called device dependent resources because they are stored on the GPU. Other Direct2D resources (a stroke or a mesh for instance) are not stored on the GPU, they are device independent resources.
Depending on the type of the handle argument D2GetRT invokes ID2D1Factory::CreateHwndRenderTarget or ID2D1Factory::CreateDCRenderTarget.
D2SetRT, D2Resize, D2BeginDraw, D2EndDraw
{Created by Sjouke Hamstra; Last updated: 12/12/2021 by James Gaite; Other Contributors: Jean-Marie Melanson}