Click here to Skip to main content
15,896,111 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
Class WPFDrawingProgram

    'Store a pointer to a ViewModel cthat contains all the RadioButtons
    Private ListOfRadioButtons As MainViewModel

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        'Get all the radiobuttons, and add a handler to Selection changed
        ListOfRadioButtons = DirectCast(rbtn.DataContext, MainViewModel)
        AddHandler ListOfRadioButtons.SelectionIndexUpdated, AddressOf rbtnSelectionChanged

    End Sub

    'Need to set a Property for panning the window
    Public Sub rbtnSelectionChanged(ByVal Value As Integer)
        DrawingControl.DrawingCanvas.CanvasEvent = Value
        If Value = 1 Then
            DrawingControl.DrawingScroll.Hand = True
        Else
            DrawingControl.DrawingScroll.Hand = False
        End If
    End Sub

    Private Sub btn_AddPicture_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btn_AddPicture.Click
        Dim dlg As New System.Windows.Forms.OpenFileDialog
        With dlg
            If .ShowDialog = Forms.DialogResult.OK Then
                'No filter here. If its not an image youll get an exception here
                DrawingControl.AddPicture(.FileName)
            End If
        End With
    End Sub

    Private Sub Window_MouseMove(sender As System.Object, e As System.Windows.Input.MouseEventArgs) Handles MyBase.MouseMove
        txtPosition.Text = e.GetPosition(DrawingControl.DrawingScroll).ToString ' DrawingControl.DrawingCanvas.MousePosition
    End Sub

    Private Sub btn_GenerateXAML_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btn_GenerateXAML.Click
        'Create a XML document
        Dim XMLdocument As New XDocument

        'Create an parent element for the XML document
        Dim nodeInXMLDocument As New XElement("File")

        'All the custom controls on the DrawingCanvas inherits FrameWorkelement 
        For Each ElementsInDrawingCanvas As FrameworkElement In DrawingControl.DrawingCanvas.Children
            If TypeOf (ElementsInDrawingCanvas) Is CustomLine Then
                nodeInXMLDocument.Add(FileHandling.CreateXAMLfromLine(ElementsInDrawingCanvas))
            ElseIf TypeOf (ElementsInDrawingCanvas) Is CustomPolygon Then
                nodeInXMLDocument.Add(FileHandling.CreateXAMLfromPolygon(ElementsInDrawingCanvas))
            End If
        Next
        'Add the elemnts to file
        XMLdocument.Add(nodeInXMLDocument)

        'Show the new dialog
        Dim dlg As New XAMLViewer(XMLdocument)
        dlg.ShowDialog()
    End Sub

    Private Sub Minimize_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Me.WindowState = Windows.WindowState.Minimized
    End Sub

    Private Sub Maximize_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        If Me.WindowState = Windows.WindowState.Maximized Then
            Me.WindowState = Windows.WindowState.Normal
        Else
            Me.WindowState = Windows.WindowState.Maximized
        End If
    End Sub

    Private Sub Exit_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Application.Current.Shutdown()
    End Sub

    Private Sub StackPanel_MouseLeftButtonDown(sender As System.Object, e As System.Windows.Input.MouseButtonEventArgs)
        Me.DragMove()
    End Sub

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