Click here to Skip to main content
15,896,201 members
Articles / Programming Languages / Visual Basic

WPF-Drawing Canvas Control

Rate me:
Please Sign up or sign in to vote.
4.93/5 (13 votes)
5 Oct 2012CPOL13 min read 124.7K   8.1K   47  
A drawing tool program that can create simplified XAML code
Imports System.Globalization

Public Class CanvasPoint
    Inherits FrameworkElement

    Implements System.ComponentModel.INotifyPropertyChanged

    ' Create a collection of child visual objects.
    Private _children As VisualCollection

#Region "Constructors"
    Public Sub New()
        _children = New VisualCollection(Me)
        _children.Add(CreateDrawingVisualCircle())

    End Sub

    Public Sub New(p_position As Point)
        PositionOnCanvas = p_position

        _children = New VisualCollection(Me)
        _children.Add(CreateDrawingVisualCircle())
    End Sub
#End Region

#Region "Properties"

    Private p_PositionOnCanvas As New Point
    Public Property PositionOnCanvas() As Point
        Get
            Return p_PositionOnCanvas
        End Get
        Set(ByVal value As Point)
            p_PositionOnCanvas = value
            INotifyChange("PositionOnCanvas")
        End Set
    End Property
#End Region

#Region "Overided properties"

    ' Provide a required override for the VisualChildrenCount property.
    Protected Overrides ReadOnly Property VisualChildrenCount() As Integer
        Get
            Return _children.Count
        End Get
    End Property

    ' Provide a required override for the GetVisualChild method.
    Protected Overrides Function GetVisualChild(ByVal index As Integer) As Visual
        If index < 0 OrElse index >= _children.Count Then
            Throw New ArgumentOutOfRangeException()
        End If

        Return _children(index)
    End Function

#End Region

#Region "Drawing"
    ' Create a DrawingVisual that contains a rectangle.
    Private Function CreateDrawingVisualCircle() As DrawingVisual
        Dim drawingVisual As New DrawingVisual()

        ' Retrieve the DrawingContext in order to create new drawing content.
        Dim drawingContext As DrawingContext = drawingVisual.RenderOpen()

        ' Create a circle and draw it in the DrawingContext.
        drawingContext.DrawEllipse(Brushes.Red, New Pen(Brushes.Red, 1.0), PositionOnCanvas, 4, 4)

        ' Persist the drawing content.
        drawingContext.Close()

        Return drawingVisual
    End Function

#End Region

#Region "Events"
    Public Sub INotifyChange(ByVal info As String)
        RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs(info))
    End Sub

    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
#End Region

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
Norway Norway
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions