WPF 后台代码:鼠标左键捕获点,用polyline连接起来,实现个让线跟着鼠标移动而实现画线的过程 求代码?
展开全部
坐等答案出来,我对这个也很感兴趣。
在窗体里添加一个Canvas 控件 命名为canvas
添加canvas控件的MouseLeftButtonDown、MouseRightButtonDown、MouseMove事件
我的窗体类名为Window1
Polyline lines;
Line dot;
Polyline cach;
帆做野 public Window1()
{
态喊 InitializeComponent();
lines=new Polyline();
lines.Stroke =new SolidColorBrush(Colors.Black);
胡纯 canvas.Children.Add(lines);
cach=new Polyline();
cach.Stroke =new SolidColorBrush(Colors.Black);
canvas.Children.Add(cach);
dot=new Line();
dot.Stroke =new SolidColorBrush(Colors.Black);
dot.StrokeDashArray.Add(4);
canvas.Children.Add(dot);
}
void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var point= e.GetPosition(canvas);
lines.Points.Add(point);
}
void Canvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
cach.Points=lines.Points.Clone();
cach.Points.Add(lines.Points[0]);
lines.Points.Clear();
dot.X1=dot.X2=dot.Y1=dot.Y2=0;
}
void Canvas_MouseMove(object sender, MouseEventArgs e)
{
if (lines.Points.Count>0) {
var point=lines.Points[lines.Points.Count-1];
dot.X1=point.X;
dot.Y1=point.Y;
point= e.GetPosition(canvas);
dot.X2=point.X;
dot.Y2=point.Y;
}
}
展开全部
这么多分,给你写一个。完美,分给我给我给我
bool flag = false;
private void canvas_MouseMove(object sender, MouseEventArgs e)
{
if (flag == false)
磨祥 return;
polyline.Points[polyline.Points.Count - 1] = e.GetPosition(canvas);
Console.WriteLine("Move: " + polyline.Points[polyline.Points.Count - 1]);
}
private void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
flag = true;
polyline.Points.Add(e.GetPosition(canvas));
掘纤 if (polyline.Points.Count == 1)
判游仿 polyline.Points.Add(e.GetPosition(canvas));
Console.WriteLine("Left Down: " + e.GetPosition(canvas));
}
private void canvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
flag = false;
Console.WriteLine("Right Down: " + e.GetPosition(canvas));
}
<Canvas Name="canvas" MouseMove="canvas_MouseMove" MouseLeftButtonDown="canvas_MouseLeftButtonDown" MouseRightButtonDown="canvas_MouseRightButtonDown" Background="#FF4787E8">
<Polyline Name="polyline" Stroke="Red" StrokeThickness="3">
</Polyline>
</Canvas>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询