Class CanvasControl
- Namespace
- SharpConsoleUI.Controls
- Assembly
- SharpConsoleUI.dll
A free-form drawing surface control that exposes CharacterBuffer drawing primitives through a local-coordinate API. Supports both async/on-demand painting via BeginPaint()/EndPaint() and event-driven painting via the Paint event. Both modes can be combined.
public class CanvasControl : BaseControl, IDOMPaintable, INotifyPropertyChanged, IInteractiveControl, IFocusableControl, IMouseAwareControl, IWindowControl, IDisposable
- Inheritance
-
CanvasControl
- Implements
- Inherited Members
- Extension Methods
Constructors
CanvasControl()
Initializes a new CanvasControl with default dimensions.
public CanvasControl()
CanvasControl(int, int)
Initializes a new CanvasControl with the specified dimensions.
public CanvasControl(int canvasWidth, int canvasHeight)
Parameters
canvasWidthintThe initial canvas width in characters.
canvasHeightintThe initial canvas height in characters.
Properties
AutoClear
When true, the internal buffer is cleared after compositing each frame, so the Paint event redraws from scratch. Default is false (retained mode).
public bool AutoClear { get; set; }
Property Value
AutoSize
When true, the internal buffer automatically resizes to match the layout bounds assigned by the parent container. Enable this when using Stretch/Fill alignment so the canvas adapts to the available window space.
public bool AutoSize { get; set; }
Property Value
BackgroundColor
Gets or sets the background color. Falls back to container, then black.
public Color BackgroundColor { get; set; }
Property Value
CanFocusWithMouse
Whether this control can receive focus via mouse clicks
public bool CanFocusWithMouse { get; }
Property Value
CanReceiveFocus
Whether this control can receive focus
public bool CanReceiveFocus { get; }
Property Value
CanvasHeight
Gets or sets the canvas height in characters. Clamped to MinCanvasSize. Setting this recreates the internal buffer (previous content is lost).
public int CanvasHeight { get; set; }
Property Value
CanvasWidth
Gets or sets the canvas width in characters. Clamped to MinCanvasSize. Setting this recreates the internal buffer (previous content is lost).
public int CanvasWidth { get; set; }
Property Value
ContentWidth
Gets the minimum width needed to display the control's content, including margins. Returns null if width cannot be determined. This is calculated based on content (text length, child controls, etc.) and represents the natural/intrinsic size.
public override int? ContentWidth { get; }
Property Value
- int?
ForegroundColor
Gets or sets the foreground color. Falls back to container, then white.
public Color ForegroundColor { get; set; }
Property Value
HasFocus
public bool HasFocus { get; }
Property Value
IsEnabled
Gets or sets whether the control is enabled and can receive input.
public bool IsEnabled { get; set; }
Property Value
WantsMouseEvents
Whether this control wants to receive mouse events
public bool WantsMouseEvents { get; }
Property Value
Methods
BeginPaint()
Returns a CanvasGraphics wrapping the internal buffer for async drawing. Must be paired with EndPaint(). Thread-safe via monitor lock.
public CanvasGraphics BeginPaint()
Returns
- CanvasGraphics
A drawing context for the internal canvas buffer.
Clear()
Clears the internal buffer with the control's background color.
public void Clear()
Clear(Color)
Clears the internal buffer with the specified background color.
public void Clear(Color bg)
Parameters
bgColorThe background color to fill with.
EndPaint()
Releases the paint lock and triggers a repaint.
public void EndPaint()
MeasureDOM(LayoutConstraints)
Measures the control's desired size given the available constraints.
public override LayoutSize MeasureDOM(LayoutConstraints constraints)
Parameters
constraintsLayoutConstraintsThe layout constraints (min/max width/height).
Returns
- LayoutSize
The desired size of the control.
OnDisposing()
Called during Dispose() before Container is set to null.
Override to perform control-specific cleanup (null events, close portals, clear data, etc.).
protected override void OnDisposing()
PaintDOM(CharacterBuffer, LayoutRect, LayoutRect, Color, Color)
Paints the control's content directly to a CharacterBuffer.
public override void PaintDOM(CharacterBuffer buffer, LayoutRect bounds, LayoutRect clipRect, Color defaultForeground, Color defaultBackground)
Parameters
bufferCharacterBufferThe buffer to paint to.
boundsLayoutRectThe absolute bounds where the control should paint.
clipRectLayoutRectThe clipping rectangle (visible area).
defaultForegroundColorThe default foreground color from the container.
defaultBackgroundColorThe default background color from the container.
ProcessKey(ConsoleKeyInfo)
Processes a keyboard input event.
public bool ProcessKey(ConsoleKeyInfo key)
Parameters
keyConsoleKeyInfoThe key information for the pressed key.
Returns
- bool
True if the key was handled by this control; otherwise, false.
ProcessMouseEvent(MouseEventArgs)
Processes a mouse event for this control
public bool ProcessMouseEvent(MouseEventArgs args)
Parameters
argsMouseEventArgsMouse event arguments with control-relative coordinates
Returns
- bool
True if the event was handled and should not propagate further
Refresh()
Triggers a repaint without painting.
public void Refresh()
Events
CanvasKeyPressed
Fires when a key is pressed while the canvas has focus.
public event EventHandler<ConsoleKeyInfo>? CanvasKeyPressed
Event Type
CanvasMouseClick
Fires when the canvas is left-clicked, with canvas-local coordinates.
public event EventHandler<CanvasMouseEventArgs>? CanvasMouseClick
Event Type
CanvasMouseMove
Fires when the mouse moves over the canvas, with canvas-local coordinates.
public event EventHandler<CanvasMouseEventArgs>? CanvasMouseMove
Event Type
CanvasMouseRightClick
Fires when the canvas is right-clicked, with canvas-local coordinates.
public event EventHandler<CanvasMouseEventArgs>? CanvasMouseRightClick
Event Type
MouseClick
Event fired when the control is clicked
public event EventHandler<MouseEventArgs>? MouseClick
Event Type
MouseDoubleClick
Event fired when the control is double-clicked
public event EventHandler<MouseEventArgs>? MouseDoubleClick
Event Type
MouseEnter
Event fired when the mouse enters the control area
public event EventHandler<MouseEventArgs>? MouseEnter
Event Type
MouseLeave
Event fired when the mouse leaves the control area
public event EventHandler<MouseEventArgs>? MouseLeave
Event Type
MouseMove
Event fired when the mouse moves over the control
public event EventHandler<MouseEventArgs>? MouseMove
Event Type
MouseRightClick
Event fired when the control is right-clicked (Button3)
public event EventHandler<MouseEventArgs>? MouseRightClick
Event Type
Paint
Fires during each render cycle after the internal buffer is composited. The Graphics context draws directly to the window buffer.
public event EventHandler<CanvasPaintEventArgs>? Paint