逸尘向天 2019-05-17 10:01 采纳率: 100%
浏览 1249
已采纳

请教,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);
                }
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥15 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上