Click here to Skip to main content
11,505,744 members (60,473 online)
Click here to Skip to main content
Add your own
alternative version

WPF-Drawing Canvas Control

, 5 Oct 2012 CPOL 47.9K 4.2K 43
A drawing tool program that can create simplified XAML code
WpfDrawingCanvasProgram-noexe.zip
WpfDrawingCanvasProgram
WpfDrawingCanvasProgram.suo
WpfDrawingCanvasProgram
bin
Debug
WpfDrawingCanvasProgram.vshost.exe.manifest
Controls
BasicDrawingControls
CustomLayoutControls
FileHandling
GeometryFunctions
Images
close.png
closeMouseOver.png
Info.png
Maximize.png
MaximizeMouseOver.png
Minimize.png
MinimizeMouseOver.png
OceanWaves.jpg
My Project
MyExtensions
Settings.settings
obj
x86
Debug
Application.baml
Controls
CustomLayoutControls
DrawingCanvasControl.baml
PopupWindows
ProgressWindow.baml
XAMLViewer.baml
Resources
AppStyles.baml
WpfDrawingCanvasProgram.g.resources
WpfDrawingCanvasProgram.Resources.resources
WpfDrawingCanvasProgram_MarkupCompile.i.lref
WpfDrawingCanvasProgram_MarkupCompile.lref
WPFDrawingProgram.baml
PopupWindows
Resources
ViewModels
WpfDrawingCanvasProgram.vbproj.user
WpfDrawingCanvasProgram.zip
WpfDrawingCanvasProgram.suo
WpfDrawingCanvasProgram.exe
WpfDrawingCanvasProgram.vshost.exe
WpfDrawingCanvasProgram.vshost.exe.manifest
close.png
closeMouseOver.png
Info.png
Maximize.png
MaximizeMouseOver.png
Minimize.png
MinimizeMouseOver.png
OceanWaves.jpg
Settings.settings
Application.baml
DrawingCanvasControl.baml
ProgressWindow.baml
XAMLViewer.baml
AppStyles.baml
TempPE
My Project.Resources.Designer.vb.dll
WpfDrawingCanvasProgram.exe
WpfDrawingCanvasProgram.g.resources
WpfDrawingCanvasProgram.Resources.resources
WpfDrawingCanvasProgram_MarkupCompile.i.lref
WpfDrawingCanvasProgram_MarkupCompile.lref
WPFDrawingProgram.baml
WpfDrawingCanvasProgram.vbproj.user
Public Class DrawingCanvasControl
    Sub New()

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

        ' Add any initialization after the InitializeComponent() call.
        AddHandler DrawingCanvas.NewRectangleZoom, AddressOf ChangeRectangleZoom
    End Sub

    'Is the mouse over the Rezsize border?
    Private MouseOnResizeContainer As Boolean = False

    'Stores the scaling
    Dim scale As Double = 1

    Private Sub CanvasContainer_SizeChanged(sender As System.Object, e As System.Windows.SizeChangedEventArgs) Handles Me.SizeChanged
        If MouseOnResizeContainer Then
            DrawingCanvas.ChangeSize((CanvasContainer.Height - 10) / scale, (CanvasContainer.Width - 10) / scale)
        End If
    End Sub

    Public Sub ResetZoom()
        DrawingScroll.ScrollFromCode()

        scale = 1
        Dim ScalingTheCanvas As New ScaleTransform(scale, scale)

        'Scaling the DrawingCanvas
        DrawingCanvas.LayoutTransform = ScalingTheCanvas

        'Adjusting the resizer decorator according to the new Height and Width of the canvas
        CanvasContainer.Width = DrawingCanvas.Width * scale + 10
        CanvasContainer.Height = DrawingCanvas.Height * scale + 10
        DrawingScroll.ScrollToHorizontalOffset(0)
        DrawingScroll.ScrollToVerticalOffset(0)
    End Sub

    Public Sub ChangeRectangleZoom(ByVal TopLeft As Point, ByVal BottomRight As Point)
        Dim _width, _height As Double
        _height = Math.Abs(TopLeft.Y - BottomRight.Y)
        _width = Math.Abs(TopLeft.X - BottomRight.X)

        If DrawingScroll.ViewportWidth / _width > DrawingScroll.ViewportHeight / _height Then
            scale = DrawingScroll.ViewportHeight / _height
        Else
            scale = DrawingScroll.ViewportWidth / _width
        End If

        DrawingScroll.ScrollFromCode()
        Dim ScalingTheCanvas As New ScaleTransform(scale, scale)

        'Scaling the DrawingCanvas
        DrawingCanvas.LayoutTransform = ScalingTheCanvas

        'Adjusting the resizer decorator according to the new Height and Width of the canvas
        CanvasContainer.Width = DrawingCanvas.Width * scale + 10
        CanvasContainer.Height = DrawingCanvas.Height * scale + 10

        'The new point is oldpoint*scale, and we add 10 as we have not scaled the Canvas Container... The border around this is constant
        DrawingScroll.ScrollToHorizontalOffset(scale * TopLeft.X + _width / 2 + 10)
        DrawingScroll.ScrollToVerticalOffset(scale * TopLeft.Y + _height / 2 + 10)
    End Sub

    ''' <summary>
    ''' Adds a background picture to the canvas. The picture is added as a UIElement. 
    ''' </summary>
    ''' <param name="pic">The url of the picture you want to add</param>
    ''' <remarks>You could also use an ImageBrush but that has problems in resizing... </remarks>
    Public Sub AddPicture(ByVal pic As String)
        'Create a new image element from the selected file
        Dim img As New PictureElement(New Point(0, 0), pic)

        'Add the image element to the DrawingCanvas
        DrawingCanvas.Children.Add(img)

        'It dosnt measure the child elements befor it is drawn
        DrawingCanvas.Width = img.ImageWidth
        DrawingCanvas.Height = img.ImageHeight

        'Adjusting the resizer decorator according to the new Height and Width of the canvas
        CanvasContainer.Width = DrawingCanvas.Width * scale + 10
        CanvasContainer.Height = DrawingCanvas.Height * scale + 10

        'Setting the picture to be the lowest Z-Index. This to allow other elements above it.
        Canvas.SetZIndex(DrawingCanvas.Children(DrawingCanvas.Children.Count - 1), -1)
    End Sub

    Private Sub UserControl_PreviewMouseWheel(sender As System.Object, e As System.Windows.Input.MouseWheelEventArgs) Handles Me.PreviewMouseWheel
            If Not MouseOnResizeContainer And DrawingCanvas.SelectedElement Is DrawingCanvas Then

                'Creating And Storing The scalefactor
                Dim scalefactor As Double = 0


                'Transform the MouseWheel to the scalefactor
                If e.Delta > 0 Then
                    scalefactor = 0.1
                ElseIf e.Delta < 0 Then
                    scalefactor = -0.1
                End If

                'The sc ale cannot be negative
                If scale + scalefactor < 0 Then
                    Exit Sub
                End If

                'Adjusting the old scale
                scale = scale + scalefactor

                'Creating a new scaletransform with the new scale values
                Dim ScalingTheCanvas As New ScaleTransform(scale, scale)

                'Scaling the DrawingCanvas
                DrawingCanvas.LayoutTransform = ScalingTheCanvas

                'Adjusting the resizer decorator according to the new Height and Width of the canvas
            CanvasContainer.Width = DrawingCanvas.ActualWidth * scale + 10
            CanvasContainer.Height = DrawingCanvas.ActualHeight * scale + 10

            ElseIf DrawingCanvas.SelectedElement IsNot DrawingCanvas And (Keyboard.IsKeyDown(Key.LeftCtrl) Or Keyboard.IsKeyDown(Key.RightCtrl)) Then
                DrawingCanvas.ChangeSizeOfElement(e)
            End If

            'The mouse wheel action will be stoped here in any case
            e.Handled = True

    End Sub

    Private Sub ResizeThumb_MouseEnter(sender As System.Object, e As System.Windows.Input.MouseEventArgs)
        MouseOnResizeContainer = True
    End Sub

    Private Sub ResizeThumb_MouseLeave(sender As System.Object, e As System.Windows.Input.MouseEventArgs)
        MouseOnResizeContainer = False
    End Sub

    Private Sub Border_KeyDown(sender As System.Object, e As System.Windows.Input.KeyEventArgs)
        If e.Key = Key.Escape Then
            ResetZoom()
            Exit Sub
        End If
        'The canvas has problems handeling the key down event, so its passed on from the border
        DrawingCanvas.CanvasDraw_KeyDown(sender, e)
    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)

Share

About the Author

Kenneth Haugland
Engineer
Norway Norway
I hope that you like the stuff I have created and if you do wish to say thank you then a donation is always appreciated.
You can donate here[^].

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 6 Oct 2012
Article Copyright 2012 by Kenneth Haugland
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid