逸尘向天
2019-05-17 10:01
采纳率: 66.7%
浏览 993

请教,C# winform根据鼠标移动画多条直线

各位大佬,麻烦帮我看看哪里不对劲,根据MouseDowm、MouseMove、MouseUp画下直线,但再画另一条之前画的一条就消失了。请教怎么画多条直线?只画直线就行
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if(startDraw)
{
startpoint.X=e.X;
startpoint.Y= e.Y;
sdraw = true;
}
}

    private void button3_Click(object sender, EventArgs e)
    {
        startDraw = false;
    }
            private void button1_Click_1(object sender, EventArgs e)
    {
        startDraw = true;//点击按钮鼠标处于画线状态
    }

    private void panel1_MouseUp(object sender, MouseEventArgs e)
    {
        if (startDraw)
        {
            sdraw = false ;
        }
    }

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        if (startDraw)
        {
            if (sdraw)
            {
                currentpoint.X = e.X;
                currentpoint.Y = e.Y;
                Refresh();
            }
        }
                    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        Graphics newgraphics = e.Graphics; 
        Rectangle r = new Rectangle(0, 0, 795, 400);//是创建画矩形的区域 
        newgraphics.DrawRectangle(Pens.Red, r);
        if (sdraw && startDraw)
        {
            newgraphics.DrawLine(p, startpoint, currentpoint);
        }
    }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 文氓 2019-05-17 11:38
    已采纳

    将画线的起始点和结束点保存下来,下次画线的时候将之前所画线重新绘制出来,笨办法,不要吐槽

     bool startDraw = true;
            bool sdraw = false;
            private Point currentpoint = new Point();
            private Point startpoint = new Point();
            private Pen p = new Pen(Color.Red, 12);
            List<Point> startPoints = new List<Point>();
            List<Point> endPoints = new List<Point>();
            public Form1()
            {
                InitializeComponent();
            }
            private void panel1_MouseDown(object sender, MouseEventArgs e)
            {
                if (startDraw)
                {
                    startpoint.X = e.X;
                    startpoint.Y = e.Y;
                    startPoints.Add(startpoint);
                    sdraw = true;
                }
            }
            private void button3_Click(object sender, EventArgs e)
            {
                startDraw = false;
            }
            private void button1_Click_1(object sender, EventArgs e)
            {
                startDraw = true;//点击按钮鼠标处于画线状态
            }
    
            private void panel1_MouseUp(object sender, MouseEventArgs e)
            {
                if (startDraw)
                {
                    endPoints.Add(currentpoint);
                    sdraw = false;
                }
            }
    
            private void panel1_MouseMove(object sender, MouseEventArgs e)
            {
                if (startDraw)
                {
                    if (sdraw)
                    {
                        currentpoint.X = e.X;
                        currentpoint.Y = e.Y;
                        Refresh();
                    }
                }
            }
            private void panel1_Paint(object sender, PaintEventArgs e)
            {
                Graphics newgraphics = e.Graphics;
                Rectangle r = new Rectangle(0, 0, 795, 400);//是创建画矩形的区域 
                newgraphics.DrawRectangle(Pens.Red, r);
                if (sdraw && startDraw)
                {
                    for (int i = 0; i < startPoints.Count; i++)
                    {
                        if (i<endPoints.Count)
                        {
                            newgraphics.DrawLine(p, startPoints[i], endPoints[i]); 
                        }
                    }
                    newgraphics.DrawLine(p, startpoint, currentpoint);
                }
            }
    
    点赞 打赏 评论

相关推荐 更多相似问题