How to handle thumbs with a line in custom Adorner WPF

I am working on Adorner for a string in a drawing program using WPF. The line is drawn in code, and then decorated with my usual Adorner, which is called LineAdorner. I managed to use the ThumbLines for the start and end points. My problem is the location of the Thumbs relative to the start and end points. I think the problem lies in the method ArrangeOverridewhere it is supposed to arrange Thumbs with start and end points. I can not find the right amount to subtract or add to the parameters Rect Xand Y. How can I find these values ​​to always arrange Thumbs with line points? My custom Adorner code:

public class LineAdorner : Adorner  
{
    private Point start;
    private Point end;
    private Thumb startThumb;
    private Thumb endThumb;
    private Line selectedLine;
    private VisualCollection visualChildren;

    // Constructor
    public LineAdorner(UIElement adornedElement) : base(adornedElement)
    {
        visualChildren = new VisualCollection(this);

        startThumb = new Thumb { Cursor = Cursors.Hand, Width = 10, Height = 10, Background = Brushes.Green };
        endThumb   = new Thumb { Cursor = Cursors.Hand, Width = 10, Height = 10, Background = Brushes.BlueViolet };

         startThumb.DragDelta += StartDragDelta;
         endThumb.DragDelta   += EndDragDelta;

         visualChildren.Add(startThumb);
         visualChildren.Add(endThumb);

         selectedLine = AdornedElement as Line;
     }

     // Event for the Thumb Start Point
     private void StartDragDelta(object sender, DragDeltaEventArgs e)
     {
        Point position = Mouse.GetPosition(this);

        selectedLine.X1 = position.X;
        selectedLine.Y1 = position.Y;
     }

     // Event for the Thumb End Point
     private void EndDragDelta(object sender, DragDeltaEventArgs e)
     {
         Point position = Mouse.GetPosition(this);

         selectedLine.X2 = position.X;
         selectedLine.Y2 = position.Y;
     }

     protected override int VisualChildrenCount { get { return visualChildren.Count; } }
     protected override Visual GetVisualChild(int index) { return visualChildren[index]; }

     protected override void OnRender(DrawingContext drawingContext)
     {
         if (AdornedElement is Line)
         {
             selectedLine = AdornedElement as Line;
             start = new Point(selectedLine.X1, selectedLine.Y1);
             end   = new Point(selectedLine.X2, selectedLine.Y2);
          }
     }

     protected override Size ArrangeOverride(Size finalSize)
     {
         var startRect = new Rect(selectedLine.X1, selectedLine.Y1, ActualWidth, ActualHeight);
         startThumb.Arrange(startRect);

         var endRect = new Rect(selectedLine.X2, selectedLine.Y2, ActualWidth, ActualHeight);
         endThumb.Arrange(endRect);

         return finalSize;
     }
    }

+5
source share
1

ArrangeOverride. "start" "end", OnRender, Thumbs , , .

    protected override Size ArrangeOverride(Size finalSize)
{
    selectedLine = AdornedElement as Line;

    double left = Math.Min(selectedLine.X1, selectedLine.X2);
    double top = Math.Min(selectedLine.Y1, selectedLine.Y2);

    var startRect = new Rect(selectedLine.X1 - (startThumb.Width / 2), selectedLine.Y1 - (startThumb.Width / 2), startThumb.Width, startThumb.Height);
    startThumb.Arrange(startRect);

    var endRect = new Rect(selectedLine.X2 - (endThumb.Width / 2), selectedLine.Y2 - (endThumb.Height / 2), endThumb.Width, endThumb.Height);
    endThumb.Arrange(endRect);

    return finalSize;
}

Thumbs, Arrange. , Thumbs, .

- "" , "", "" , Canvasses.

+5

All Articles