D2PSet command

Requires: Direct2D.lg32

Purpose

Draws a square dot using the specified ARGB color or a brush created from the the current foreground color.

Syntax

D2PSet x!, y! [,argbcolor]

x!, y!: float expression
argbcolor: long expression

Description

Draws a square dot at x,y in device-independent pixels. By default, the dot is drawn using a solid color brush created from the current foreground color D2ForeColor. If the application passes an ARGB color value the dot is painted with a brush based on that color.

Example

'

' Samples\Direct2D\D2PSet.g32 sample

' (dpi-aware)

'

$Library "direct2d"

$Library "gfawinx"

DpiAwareness()

Global Object Win1RT

Global Long DotColor = 0

' A window of 320x260 pixels on 96 dots-per-inch:

OpenW 1, 0, 0, 320, 260, 48     ' no size attribute

FormScaleForDpi(Me)

' Get a rendertarget (and initialize Direct2D):

Set Win1RT = D2GetRT()

Do

Sleep

Until Me Is Nothing

 

Sub Win_1_Paint

' Clear the window render target

D2BeginDraw Win1RT

D2Clear D2C_Yellow

D2Text 0, 0, "Move mouse over window"

D2EndDraw

EndSub

 

Sub Win_1_MouseMove(Button&, Shift&, x!, y!)

x! *= ScaleMX, y! *= ScaleMY  ' x,y in ScaleWidth/Height units to pixels

D2BeginDraw Win1RT

D2PSet D2PixelsToDIP(x!), D2PixelsToDIP(y!), DotColor

D2EndDraw

EndSub

 

Sub Win_1_MouseDown(Button&, Shift&, x!, y!)

DotColor = Rand(_maxInt)    ' change color

EndSub

 

Sub Win_1_ReSize

D2ResizeRT Win1RT, _X, _Y    ' new size of RT

EndSub

 

Sub Win_1_MessageProc(hWnd%, Mess%, wParam%, lParam%, retval%, ValidRet?)

Local Int dpi

If Mess% == WM_DPICHANGED

If FormScaleForDpi(Me, wParam%, lParam%' scale (resize) form and it's Ocx-es

' Here: Update other Dpi dependent resources (toolbar, bitmaps, Direct2D)

dpi = LoWord(wParam%)             ' new Dpi for form

D2SetDpiRT Win1RT, dpi            ' update D2 rendertarget's dpi

EndIf

retval% = 0 : ValidRet? = True    ' tell OS we handled it

EndIf

EndSub

 

Function FormScaleForDpi(ByVal frm As Form, Optional newdpi As Int, Optional lParam As Int) As Bool

Local i As Int, c As Control, rc As RECT, prevDpi As Int, newrc As Pointer To RECT, _

tb As Tab, w As Int, h As Int

With frm

' Set initial newdpi for the form and the scale factors for a LoadForm

If .WhatsThisHelpID == 0

If Exist(":{" + .Name)                    ' a Form-Editor form

.WhatsThisHelpID = WinDpi(0)            ' initial dpi is system newdpi

.ScaleWidth = .ScaleWidth * 96 / .WhatsThisHelpID   ' set initial scale factors

.ScaleHeight = .ScaleHeight * 96 / .WhatsThisHelpID

Else                                      ' created by OpenW or Form command

.WhatsThisHelpID = 96                   ' coord space is 96

EndIf

EndIf

prevDpi = .WhatsThisHelpID

newdpi = LoWord(newdpi)

If newdpi == 0 Then newdpi = WinDpi(.hWnd)

Exit Func If newdpi == prevDpi              ' don't continue if equal

' Size the form, except if it is a child window

If !(GetWindowLong(.hWnd, GWL_STYLE) %& WS_CHILD)

If lParam                                 ' size to the OS suggested size

Pointer newrc = lParam                  ' ptr to RECT in lParam

SizeW .hWnd, newrc.Right - newrc.Left, newrc.Bottom - newrc.Top

Else                                      ' scale based on the current client size

GetClientSize .hWnd, w, h               ' can't use _X _Y, they are for ME only

AdjustW .hWnd, Scale(w, newdpi, prevDpi), Scale(h, newdpi, prevDpi)

EndIf

EndIf

.FontSize = .FontSize * newdpi / prevDpi

.ScaleWidth = .ScaleWidth * prevDpi / newdpi

.ScaleHeight = .ScaleHeight * prevDpi / newdpi

' Scale position and size of the Ocx windowed controls

For Each c In frm.Controls

If !(TypeOf(c) Is ImageList || TypeOf(c) Is Timer || _

TypeOf(c) Is CommDlg || TypeOf(c) Is TrayIcon' not the windowless OCX-es

' First set fontsize, but not all OCX-es have a Font property

If !(TypeOf(c) Is ProgressBar || TypeOf(c) Is Slider || TypeOf(c) Is Image || TypeOf(c) Is RichEdit)

c.fontsize = c.fontsize * newdpi / prevDpi

ElseIf TypeOf(c) Is RichEdit            ' Requires LoadRichEdit50W!

SendMessage c.hwnd, WM_DPICHANGED, MakeLong(newdpi, newdpi), 0

EndIf

' Set new position and size for OCX-controls

GetWinPos(c.hwnd, rc)                   ' relative to parent

~SetWindowPos(c.hwnd, 0, _

Scale(rc.Left, newdpi, prevDpi), Scale(rc.Top, newdpi, prevDpi), _

Scale(rc.Right - rc.Left, newdpi, prevDpi), _

Scale(rc.Bottom - rc.Top, newdpi, prevDpi),  SWP_NOZORDER) ' new pos and size

If TypeOf(c) Is Form                    ' Scale OCX-es of a child Form Ocx

c.WhatsThisHelpID = prevDpi

~FormScaleForDpi(c, newdpi)

ElseIf TypeOf(c) Is TabStrip            ' Scale TabStrip's attached forms

For Each tb In c.Tabs

If TypeOf(tb.Ocx) Is Form

tb.Ocx.WhatsThisHelpID = prevDpi

~FormScaleForDpi(tb.Ocx, newdpi)

EndIf

Next

EndIf

EndIf

Next

.WhatsThisHelpID = newdpi     ' store new newdpi

EndWith

FormScaleForDpi = True

EndFunc

Remarks

D2PSet invokes ID2D1RenderTarget::DrawLine passing the coordinates x!,y!,x!+1,y!+1 and a line width of 1 DIP.

D2Plot draws a rounded dot.

All drawing commands must be placed between D2BeginDraw and D2EndDraw.

See Also

D2Plot, D2RGBColor, D2Color

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