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

请教,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 03: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);
                }
            }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥100 求一个c#通过CH341读取数据的Demo,能够读取指定地址值的功能
  • ¥15 Matlab图例修改
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
  • ¥15 Python中关于代码运行报错的问题
  • ¥500 python 的API,有酬谢
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部