请教,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个回答

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

 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);
            }
        }
wojteck
逸尘向天 能成功就行,十分感谢大佬
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐