簡體   English   中英

移動折線WPF

[英]Moving Polyline WPF

我想為所有程序移動一個加號,並獲得該符號中心的點。 加號是一個形狀,在畫布中:

<Canvas x:Name="canvas1" PreviewMouseLeftButtonDown="canvas1_PreviewMouseDown" 
        PreviewMouseMove="canvas1_PreviewMouseMove"
        PreviewMouseLeftButtonUp="canvas1_PreviewMouseUp" Grid.ColumnSpan="2" Margin="0,0,10,0">
        <Polyline Name="myPolyline"
          MouseDown="Polyline_MouseDown"
          Points="25,0 25,50 25,25 0,25 50,25" 
          Stroke="Blue"
          StrokeThickness="2"
          Height="50"/>
    </Canvas>

我的方法:

private void canvas1_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        //Check if the click was in a chape
        if (e.Source is Shape)
        {
            // Get the mouse position
            start = e.GetPosition(canvas1);
            // Initialize some components and set opacity to 50%
            isDragging = true;
            movedElement = (Shape)e.Source;
            ((Shape)e.Source).Opacity = 0.5;
            canvas1.CaptureMouse();
            e.Handled = true;
        }
    }

    private void canvas1_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        if (isDragging)
        {
            Point Pt = e.GetPosition(canvas1);
            // Get the actual position of the Shape
            double CurrentLeft = (double)movedElement.GetValue(Canvas.LeftProperty);
            double CurrentTop = (double)movedElement.GetValue(Canvas.TopProperty);

            // Calc the new position
            double newLeft = CurrentLeft + Pt.X - start.X;
            double newTop = CurrentTop + Pt.Y - start.Y;

            // Move the element
            movedElement.SetValue(Canvas.LeftProperty, newLeft);
            movedElement.SetValue(Canvas.TopProperty, newTop);

            start = Pt;
            e.Handled = true;
        }
    }

    private void canvas1_PreviewMouseUp(object sender, MouseButtonEventArgs e)
    {
        // Restore the values
        movedElement.Opacity = 1;
        movedElement.SetValue(Canvas.ZIndexProperty, ++currentZ);
        isDragging = false;
        canvas1.ReleaseMouseCapture();
    }

我找到了一些可以完美地移動直線,矩形和橢圓的方法,但是我的加號是折線,但是這些方法並沒有移動它,我相信共振線在其中:

movedElement = (Shape)e.Source; 要么

double CurrentLeft = (double)movedElement.GetValue(Canvas.LeftProperty); double CurrentTop = (double)movedElement.GetValue(Canvas.TopProperty);

因為GetValue返回“ NaN”,我真的不知道為什么。我試圖使用兩行來制作加號,但是我很難將這兩行都移到一起。任何人都可以告訴我是否存在錯誤代碼還是問題在於我的加號類型? 謝謝。

您需要在XAML中初始化Canvas.Left和Canvas.Top屬性,然后才能在后面的代碼中查詢它們。 默認情況下為空。 例:

<Polyline Name="myPolyline"
    ...
    Canvas.Left="0"
    Canvas.Top="0"
    />

強調文字

enter code here
    private void canvas2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        //create polyline
        polyline1 = new Polyline();
        polyline1.StrokeThickness = 4;
        polyline1.Stroke = Brushes.Black;
        Canvas.SetTop(polyline1, 0);
        Canvas.SetLeft(polyline1, 0);
    }


    private bool isPolyLine1Drog;
    double mousePosOnPolyline1X;
    double mousePosOnPolyline1Y;
    private void Polyline1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        isPolyLine1Drog = true;
        //get distance between current mouse position and top_left polyline
        mousePosOnPolyline1X= e.GetPosition(canvas2).X-Canvas.GetLeft(polyline1);
        mousePosOnPolyline1Y = e.GetPosition(canvas2).Y-Canvas.GetTop(polyline1);
        polyline1.CaptureMouse();
    }
    private void Polyline1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        isPolyLine1Drog = false;
        polyline1.ReleaseMouseCapture();
    }
    private void Polyline1_MouseMove(object sender, MouseEventArgs e)
    {
        if (!isPolyLine1Drog) return;
        //top=top+Y changing and left=left+X changing
        double left= e.GetPosition(canvas2).X - mousePosOnPolyline1X;
        double top = e.GetPosition(canvas2).Y - mousePosOnPolyline1Y;
        Canvas.SetLeft(polyline1, left);
        Canvas.SetTop(polyline1, top);
    }//mohsen_seif@yahoo.com

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM