我想用C#编程实现连续画线功能,现在能画一条一条的线,怎么能把第一条线的终点作为第二条线的起点呢?谢

2025-06-27 07:08:07
推荐回答(1个)
回答1:

原理和楼下的一样,下面是一段完整代码,复制即可运行 using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace DrawLine { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.MouseDown+=new MouseEventHandler(Form1_MouseDown); } List PointList = new List(); float pointX, pointY; private void Form1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { pointX = e.X; pointY = e.Y; PointList.Add(new PointF(pointX, pointY)); Graphics graphics = CreateGraphics(); Pen myPen = new Pen(Color.Red); if (PointList.Count > 1) { graphics.DrawLine(myPen, PointList[PointList.Count - 2], PointList[PointList.Count - 1]); } } } } } 大概是这样,你可以往里加东西,希望对你有帮助、、、