D2Stroke function

Requires: Direct2D.lg32

Purpose

Creates a stroke object for use with D2DefLine.

Syntax

Set strokeObj = D2Stroke(strokeStyleProperties As D2D1_STROKE_STYLE_PROPERTIES, dashes() As Float, dashesCount As Long)

strokeSyleProperties: variable of type D2D1_STROKE_STYLE_PROPERTIES
dashes: array of Float
dashesCount: integer expression

Description

The D2Stroke function creates and returnes a Direct2D stroke style object. Before calling the function set the strokeStyleProperties and create a float array for the length of the dashes. If the application want the default sizes for the dashes simply declare an empty array: Dim dashes() as Float and pass the empty array to D2Stroke(). As an alternative set dashesCount to zero. If the array is not empty the dashesCount parameter must specify the number of array elements to use. When the array contains elements the dashStyle member of the D2D1_STROKE_STYLE_PROPERTIES type is over written and set to D2D1_DASH_STYLE_CUSTOM.

The strokeStyleProperties variable describes the stroke's line cap, dash offset, and other details of a stroke. The type is defined as:

Type D2D1_STROKE_STYLE_PROPERTIES

- Long startCap     ' set to D2D1_CAP_STYLE const

- Long endCap       ' set to D2D1_CAP_STYLE const

- Long dashCap      ' set to D2D1_CAP_STYLE const

- Long lineJoin     ' set to D2D1_LINE_JOIN const

- Float miterLimit  ' any value > 1.0

- Long dashStyle    ' set to D2D1_DASH_STYLE const

- Float dashOffset  ' offset to the start of the line

EndType

Set the type's members startCap, endCap, and dashCap to a predefined enumeration value defined as:

Global Enum /*D2D1_CAP_STYLE*/ _

D2D1_CAP_STYLE_FLAT = 0, _

D2D1_CAP_STYLE_SQUARE = 1, _

D2D1_CAP_STYLE_ROUND = 2, _

D2D1_CAP_STYLE_TRIANGLE = 3, _

D2D1_CAP_STYLE_FORCE_DWORD = 0xffffffff

Set the type-member dashStyle to one of the following values:

Global Enum /*D2D1_DASH_STYLE*/ _

D2D1_DASH_STYLE_SOLID = 0, _

D2D1_DASH_STYLE_DASH = 1, _

D2D1_DASH_STYLE_DOT = 2, _

D2D1_DASH_STYLE_DASH_DOT = 3, _

D2D1_DASH_STYLE_DASH_DOT_DOT = 4, _

D2D1_DASH_STYLE_CUSTOM = 5, _

D2D1_DASH_STYLE_FORCE_DWORD = 0xffffffff

Set the type-member lineJoin to one of the following values:

Global Enum /*D2D1_LINE_JOIN*/ _

D2D1_LINE_JOIN_MITER = 0, _

D2D1_LINE_JOIN_BEVEL = 1, _

D2D1_LINE_JOIN_ROUND = 2, _

D2D1_LINE_JOIN_MITER_OR_BEVEL = 3, _

D2D1_LINE_JOIN_FORCE_DWORD = 0xffffffff

A positive value for dashOffset shifts the dash pattern, in units of stroke width, toward the start of the stroke (line). A negative dash offset value shifts the dash pattern, in units of stroke width, toward the end of the stroke.

The miter specifies the limit of the thickness of the join on a mitered corner. This value is 10.0 by default and must be greater than or equal to 1.0.

Example

'

' D2Stroke sample (dpi-unaware)

'

$Library "direct2d"

Global Object Win1RT

OpenW 1, 0, 0, 320, 260, ~15

' Rendertarget same size as clientarea

Set Win1RT = D2GetRT()

' Here: Create Win_1 resources

Dim strokeStyleProp As D2D1_STROKE_STYLE_PROPERTIES

strokeStyleProp.startCap = D2D1_CAP_STYLE_ROUND

strokeStyleProp.endCap = D2D1_CAP_STYLE_ROUND

strokeStyleProp.dashCap = D2D1_CAP_STYLE_ROUND

Dim dashes(0 .. 1) As Float

' Note SDK says:

' The length of each dash and space in the dash pattern

' is the product of the element value in the array and the stroke width.

dashes(0) = 3       ' first dash 3*StrokeWidth

dashes(1) = 3       ' first space 3*StrokeWidth

' Create a stroke only once

' Using a D2Stroke object prevents the creation of a

' temporary stroke object each time 'D2DefLine style' is executed.

Global Object Stroke

Set Stroke = D2Stroke(strokeStyleProp, dashes(), 2)

Do

Sleep

Until Me Is Nothing

 

Sub Win_1_Paint

D2BeginDraw Win1RT, D2C_White

' Use global stroke object, width is 4

D2DefLine Stroke, 4

D2Line 10, 10, _X - 10, 10

D2EndDraw

EndSub

Remarks

The application may create the required stroke resources in the resource intialization code. Using a global D2Stroke object prevents the creation of a temporary stroke object each time D2DefLine is executed.

A stroke is created by calling the ID2D1Factory::CreateStrokeStyle method. Stroke objects are not part of the render target, so D2SetRT is not required. Use D2DefLine to select a new stroke as the current line style object for use with the drawing commands. The current stroke object is reset in D2EndDraw.

See Also

D2DefLine, D2DefFill, D2Brush, D2Line, D2Box, D2RBox, D2Ellipse, D2Circle

{Created by Sjouke Hamstra; Last updated: 21/03/2020 by James Gaite}