Click here to Skip to main content
12,503,334 members (60,348 online)
Click here to Skip to main content

Stats

536.8K views
43.9K downloads
615 bookmarked
Posted

WPF Diagram Designer: Part 1

, 23 Aug 2008 CPOL
Drag, resize and rotate elements on a Canvas
bin
Media
cross.png
intro.wmv
paint.png
MoveResize.exe
MoveResizeRotate.exe
MoveResizeRotateWithAdorners.exe
DiagramDesignerPart1
MoveResize
Properties
MoveResizeRotate
Properties
Resources
MoveResizeRotateWithAdorners
Adorners
Media
cross.png
intro.wmv
paint.png
Properties
Resources
DragAndResizeControl
bin
Debug
DragAndResizeControl.exe
DragAndResizeControl.csproj.user
Properties
DragResizeRotateItems
DragResizeRotateItems.csproj.user
media
Excavator.png
Graph.png
intro.wmv
iPod.png
Phone.png
Properties
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System;

namespace DragResizeRotateItems
{
    public class RotateThumb : Thumb
    {
        double initialAngle;
        private Vector startVector;
        private Point centerPoint;

        private ContentControl designerItem;
        private ContentControl DesignerItem
        {
            get
            {
                if (designerItem == null)
                {
                    designerItem = this.DataContext as ContentControl;
                }
                return designerItem;
            }
        }

        public RotateThumb()
        {
            base.DragDelta += new DragDeltaEventHandler(RotateThumb_DragDelta);
            base.DragStarted += new DragStartedEventHandler(RotateThumb_DragStarted);
        }

        void RotateThumb_DragStarted(object sender, DragStartedEventArgs e)
        {
            Canvas canvas = VisualTreeHelper.GetParent(DesignerItem) as Canvas;
            if (DesignerItem != null && canvas != null)
            {
                // the RenderTransformOrigin property of DesignerItem defines
                // transformation center relative to its bounds
                centerPoint = DesignerItem.TranslatePoint(
                    new Point(DesignerItem.Width * DesignerItem.RenderTransformOrigin.X,
                              DesignerItem.Height * DesignerItem.RenderTransformOrigin.Y),
                    canvas);

                // calculate startVector, that is the vector from centerPoint to startPoint
                Point startPoint = Mouse.GetPosition(canvas);
                startVector = Point.Subtract(startPoint, centerPoint);

                // check if the DesignerItem already has a RotateTransform set ...
                RotateTransform rotateTransform = DesignerItem.RenderTransform as RotateTransform;
                if (rotateTransform == null)
                {
                    // if not we create one with zero angle 
                    DesignerItem.RenderTransform = new RotateTransform(0);
                    initialAngle = 0;
                }
                else
                    initialAngle = rotateTransform.Angle;
            }
        }

        void RotateThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            Canvas canvas = VisualTreeHelper.GetParent(DesignerItem) as Canvas;

            if (DesignerItem != null && canvas != null)
            {
                // calculate deltaVector, that is the vector from centerPoint to current mouse position                
                Point currentPoint = Mouse.GetPosition(canvas);
                Vector deltaVector = Point.Subtract(currentPoint, centerPoint);

                //calculate the angle between startVector and dragVector
                double angle = Vector.AngleBetween(startVector, deltaVector);

                // and update the transformation
                RotateTransform rotateTransform = DesignerItem.RenderTransform as RotateTransform;
                rotateTransform.Angle = initialAngle + Math.Round(angle, 0);
            }
        }
    }
}

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

sukram
Austria Austria
No Biography provided

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160919.1 | Last Updated 24 Aug 2008
Article Copyright 2008 by sukram
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid