Skip to content

XCanvas

Below is the CrossBasic API for the XCanvas class.


Class: XCanvas

A double‑buffered drawing surface that you embed in an XWindow by setting its Parent. Fires paint and mouse events you can handle in script.

Dim canvas As New XCanvas

Creates a new canvas instance (unparented, with no size).


Properties

Name Type R/W Description
Left Integer Read/Write X‑coordinate (pixels) of the canvas’s top‑left corner within its parent window.
Top Integer Read/Write Y‑coordinate (pixels) of the canvas’s top‑left corner within its parent window.
Width Integer Read/Write Width in pixels; when set (after Parent), rebuilds the back‑buffer.
Height Integer Read/Write Height in pixels; when set (after Parent), rebuilds the back‑buffer.
Parent Integer Write‑only Handle of an open XWindow (from window.Handle()). Assigning creates the real Win32 control.
Graphics XGraphics Read‑only Integer handle of the internal double‑buffer for drawing.
Backdrop XPicture Read/Write Optional picture handle to draw behind your custom painting.
Paint String Read‑only Event‑token for the paint event (use with AddHandler).
MouseDown String Read‑only Event‑token fired on left‑button down (use with AddHandler).
MouseUp String Read‑only Event‑token fired on left‑button up (use with AddHandler).
MouseMove String Read‑only Event‑token fired on mouse move (use with AddHandler).
DoubleClick String Read‑only Event‑token fired on left‑button double‑click (use with AddHandler).
' Position and size
canvas.Parent = window.Handle()
canvas.Left   = 10
canvas.Top    = 10
canvas.Width  = 300
canvas.Height = 200

' Set a backdrop image
canvas.Backdrop = picture.Handle()

Methods

Refresh()

Invalidates the canvas and forces an immediate repaint (fires the paint event).

' Later, when you want to redraw:
canvas.Refresh()

Invalidate()

Alias for Refresh().

' Same as Refresh()
canvas.Invalidate()

CrossBasic provides AddressOf and AddHandler built‑ins to hook events more naturally:

Sub OnPaint(param As XGraphics)
  ' param = the internal gfx handle, e.g. "12345678"
  Dim gfx As Integer = Val(param)
  ' Draw a red rectangle via XGraphics:
  XGraphics_FillRect(gfx, 10, 10, 100, 50, &cFF0000)
End Sub

' ... after creating & parenting the canvas ...
AddHandler(canvas.Paint, AddressOf OnPaint)

You can likewise handle mouse events:

Sub OnMouseDown(param As String)
  ' param = "x,y"
  Dim parts() As String = Split(param, ",")
  Dim x As Integer = Val(parts(0))
  Dim y As Integer = Val(parts(1))
  Print "Mouse down at " + Str(x) + "," + Str(y)
End Sub

AddHandler(canvas.MouseDown, AddressOf OnMouseDown)

Constants

None.


Full Example

Sub Main()
  ' Create and parent
  Dim canvas As New XCanvas
  canvas.Parent = window.Handle()
  canvas.Left   = 20
  canvas.Top    = 20
  canvas.Width  = 400
  canvas.Height = 300

  ' Backdrop image (optional)
  canvas.Backdrop = picture.Handle()

  ' Paint handler
  AddHandler(canvas.Paint, AddressOf(OnPaint))

  ' Mouse handler
  AddHandler(canvas.MouseDown, AddressOf(OnMouseDown))
End Sub

Sub OnPaint(param As String)
  Dim gfx As Integer = Val(param)
  ' Clear to light gray
  XGraphics_FillRect(gfx, 0, 0, 400, 300, &cD3D3D3)
  ' Draw a blue circle
  XGraphics_DrawEllipse(gfx, 50, 50, 150, 150)
End Sub

Sub OnMouseDown(param As String)
  Print "Clicked at: " + param
End Sub