移动折线 WPF
Moving Polyline WPF
我想为我所有的程序移动一个加号,并得到符号中心的点。加号是一个形状,它在 canvas:
<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" 而我真的不知道 why.I 尝试使用两行来制作加号,但我很难移动这两行 toguether.Can 有人告诉我代码中是否有错误或问题出在我的加号类型上吗?
谢谢
您需要在 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
我想为我所有的程序移动一个加号,并得到符号中心的点。加号是一个形状,它在 canvas:
<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" 而我真的不知道 why.I 尝试使用两行来制作加号,但我很难移动这两行 toguether.Can 有人告诉我代码中是否有错误或问题出在我的加号类型上吗? 谢谢
您需要在 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