1

私は、WPF を使用した描画プログラムでAdornerforに取り組んでいます。LineLineコード ビハインドで描画され、 という私のカスタムで装飾さAdornerLineAdornerます。Thumbの開始点と終了点にa を使用することができましたLine。リサイズしても問題なく動いています。私の問題は、線を移動 (ドラッグ & ドロップ) できないことです。

public class ResizingAdorner : Adorner
{
    // Resizing adorner uses Thumbs for visual elements.  
    // The Thumbs have built-in mouse input handling.
    //Thumb topLeft, topRight, bottomLeft, bottomRight;

    private Thumb startThumb;
    private Thumb endThumb;
    private Line selectedLine;
    private Point startPoint;
    private Point endPoint;

    // To store and manage the adorner's visual children.
    VisualCollection visualChildren;
    bool IsControlModeOn = false;
    // Override the VisualChildrenCount and GetVisualChild properties to interface with 
    // the adorner's visual collection.
    protected override int VisualChildrenCount { get { return visualChildren.Count; } }
    protected override Visual GetVisualChild(int index) { return visualChildren[index]; }

    // Initialize the ResizingAdorner.
    public ResizingAdorner(UIElement adornedElement)
        : base(adornedElement)
    {
        visualChildren = new VisualCollection(this);
        selectedLine = AdornedElement as Line;
        startThumb = new Thumb { Cursor = Cursors.Hand, Width = 8, Height = 8, Background = Brushes.Green };
        endThumb = new Thumb { Cursor = Cursors.Hand, Width = 8, Height = 8, Background = Brushes.BlueViolet };
        startThumb.DragDelta += StartDragDelta;
        endThumb.DragDelta += EndDragDelta;

        startThumb.DragCompleted += new DragCompletedEventHandler(startThumb_DragCompleted);
        endThumb.DragCompleted += new DragCompletedEventHandler(endThumb_DragCompleted);

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


    public event EndDragDeltaEvent endDragDeltaEvent;
    public delegate void EndDragDeltaEvent(object obj, DragCompletedEventArgs e, bool isEnd);

    void startThumb_DragCompleted(object sender, DragCompletedEventArgs e)
    {
        if (endDragDeltaEvent != null)
            endDragDeltaEvent(selectedLine, e, false);
    }

    void endThumb_DragCompleted(object sender, DragCompletedEventArgs e)
    {
        if (endDragDeltaEvent != null)
            endDragDeltaEvent(selectedLine, e, true);
    }

    // Arrange the Adorners.
    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;
    }

    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 void OnRender(DrawingContext drawingContext)
    {
        if (AdornedElement is Line)
        {
            selectedLine = AdornedElement as Line;
            startPoint = new Point(selectedLine.X1, selectedLine.Y1);
            endPoint = new Point(selectedLine.X2, selectedLine.Y2);
        }
    }
}
4

1 に答える 1