Click here to Skip to main content
11,709,536 members (54,941 online)
Click here to Skip to main content
Add your own
alternative version

WPF Diagram Designer: Part 1

, 23 Aug 2008 CPOL 884.1K 37.5K 592
Drag, resize and rotate elements on a Canvas
DiagramDesignerPart1_bin.zip
bin
Media
cross.png
intro.wmv
paint.png
MoveResize.exe
MoveResizeRotate.exe
MoveResizeRotateWithAdorners.exe
DiagramDesignerPart1_src.zip
DiagramDesignerPart1
MoveResize
Properties
Settings.settings
MoveResizeRotate
Properties
Settings.settings
Resources
MoveResizeRotateWithAdorners
Adorners
Media
cross.png
intro.wmv
paint.png
Properties
Settings.settings
Resources
dragandresizecontrol.zip
DragAndResizeControl
bin
Debug
DragAndResizeControl.exe
DragAndResizeControl.csproj.user
Properties
Settings.settings
dragresizerotateitems.zip
DragResizeRotateItems
DragResizeRotateItems.csproj.user
media
Excavator.png
Graph.png
intro.wmv
iPod.png
Phone.png
Properties
Settings.settings
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...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 24 Aug 2008
Article Copyright 2008 by sukram
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid