我是WPF中的新手,正在做MyPaint应用程序。 当我在画布上画一个圆圈或一个正方形时,当我沿着Oy枢轴移动它时,它们会跟着我的鼠标。 我不知道解决这个问题。 这是。 谢谢你的阅读。
Point p1, p2;
Point currClick;
//int i = 0;
//private bool flag = true;
Rectangle Myline;
SolidColorBrush scb = new SolidColorBrush(Colors.Black);
private void MyCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
p1 = e.GetPosition(MyCanvas);
Myline = new Rectangle();
Myline.Stroke = scb;
Myline.StrokeThickness = 1;
DoubleCollection Mydash = new DoubleCollection { 5, 3 };
Myline.StrokeDashArray = Mydash;
Canvas.SetLeft(Myline, p1.X);
Canvas.SetTop(Myline, p1.Y);
//Myline.Fill = scb;
MyCanvas.Children.Add(Myline);
}
private void MyCanvas_MouseMove_1(object sender, System.Windows.Input.MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Released)
{
return;
}
else
{
Myline.Width = Math.Max(p2.X, p1.X) - Math.Min(p2.X, p1.X);
Myline.Height = Myline.Width;
Canvas.SetLeft(Myline, Math.Min(p1.X, p2.X));
Canvas.SetTop(Myline, Math.Min(p1.Y, p2.Y));
}
}
答案 0 :(得分:1)
像这样更改鼠标移动事件:
private void MyCanvas_MouseMove_1(object sender, System.Windows.Input.MouseEventArgs e)
{
p2 = e.GetPosition(MyCanvas);
if (e.LeftButton == MouseButtonState.Pressed)
{
if (p1.X < p2.X)
Canvas.SetLeft(Myline, p1.X); else Canvas.SetLeft(Myline, p2.X);
if (p1.Y < p2.Y)
Canvas.SetTop(Myline, p1.Y); else Canvas.SetTop(Myline, p2.Y);
Myline.Width = Math.Max(p2.X, p1.X) - Math.Min(p2.X, p1.X);
Myline.Height = Math.Max(p2.Y, p1.Y) - Math.Min(p2.Y, p1.Y);
}
}