Click here to Skip to main content
15,884,425 members
Articles / Desktop Programming / WPF

WPF Diagram Designer: Part 1

Rate me:
Please Sign up or sign in to vote.
4.97/5 (297 votes)
23 Aug 2008CPOL7 min read 929K   62.4K   680  
Drag, resize and rotate elements on a Canvas
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)


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

Comments and Discussions