Click here to Skip to main content
12,630,137 members (26,461 online)
Click here to Skip to main content

Stats

991.6K views
44.7K downloads
621 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;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows;

namespace DragResizeRotateItems
{
    public class ResizeThumb : Thumb
    {
        private Point transformOrigin;
        private double angle;

        private Control parentItem;
        private Control ParentItem
        {
            get
            {
                if (parentItem == null)
                {
                    parentItem = this.DataContext as Control;
                }
                return parentItem;
            }
        }

        public ResizeThumb()
        {
            base.DragStarted += new DragStartedEventHandler(ResizeThumb_DragStarted);
            base.DragDelta += new DragDeltaEventHandler(ResizeThumb_DragDelta);
        }

        void ResizeThumb_DragStarted(object sender, DragStartedEventArgs e)
        {
            if (ParentItem != null)
            {
                transformOrigin = ParentItem.RenderTransformOrigin;

                RotateTransform rotateTransform = ParentItem.RenderTransform as RotateTransform;
                if (rotateTransform != null)
                    angle = rotateTransform.Angle * Math.PI / 180.0;   //convert degrees to radians
                else
                    angle = 0.0d;
            }
        }

        void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            if (ParentItem != null)
            {
                double deltaVertical, deltaHorizontal;

                switch (base.VerticalAlignment)
                {
                    case System.Windows.VerticalAlignment.Bottom:
                        deltaVertical = Math.Min(-e.VerticalChange, ParentItem.ActualHeight - ParentItem.MinHeight);
                        Canvas.SetTop(ParentItem, Canvas.GetTop(ParentItem) + (transformOrigin.Y * deltaVertical * (1 - Math.Cos(-angle))));
                        Canvas.SetLeft(ParentItem, Canvas.GetLeft(ParentItem) - deltaVertical * transformOrigin.Y * Math.Sin(-angle));
                        ParentItem.Height -= deltaVertical;
                        break;
                    case System.Windows.VerticalAlignment.Top:
                        deltaVertical = Math.Min(e.VerticalChange, ParentItem.ActualHeight - ParentItem.MinHeight);
                        Canvas.SetTop(ParentItem, Canvas.GetTop(ParentItem) + deltaVertical * Math.Cos(-angle) + (transformOrigin.Y * deltaVertical * (1 - Math.Cos(-angle))));
                        Canvas.SetLeft(ParentItem, Canvas.GetLeft(ParentItem) + deltaVertical * Math.Sin(-angle) - (transformOrigin.Y * deltaVertical * Math.Sin(-angle)));
                        ParentItem.Height -= deltaVertical;
                        break;
                    default:
                        break;
                }

                switch (base.HorizontalAlignment)
                {
                    case System.Windows.HorizontalAlignment.Left:
                        deltaHorizontal = Math.Min(e.HorizontalChange, ParentItem.ActualWidth - ParentItem.MinWidth);
                        Canvas.SetTop(ParentItem, Canvas.GetTop(ParentItem) + deltaHorizontal * Math.Sin(angle) - transformOrigin.X * deltaHorizontal * Math.Sin(angle));
                        Canvas.SetLeft(ParentItem, Canvas.GetLeft(ParentItem) + deltaHorizontal * Math.Cos(angle) + (transformOrigin.X * deltaHorizontal * (1 - Math.Cos(angle))));
                        ParentItem.Width -= deltaHorizontal;
                        break;
                    case System.Windows.HorizontalAlignment.Right:
                        deltaHorizontal = Math.Min(-e.HorizontalChange, ParentItem.ActualWidth - ParentItem.MinWidth);
                        Canvas.SetTop(ParentItem, Canvas.GetTop(ParentItem) - transformOrigin.X * deltaHorizontal * Math.Sin(angle));
                        Canvas.SetLeft(ParentItem, Canvas.GetLeft(ParentItem) + (deltaHorizontal * transformOrigin.X * (1 - Math.Cos(angle))));
                        ParentItem.Width -= deltaHorizontal;
                        break;
                    default:
                        break;
                }
            }
            e.Handled = true;
        }
    }
}

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
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161205.3 | Last Updated 24 Aug 2008
Article Copyright 2008 by sukram
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid