我有一个List<Point>Point包含X和Y.
我想要的是循环一个这样的列表并绘制一个点对点,我这样做:
foreach (List<Point> wps in map.waypoints)
{
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(System.Drawing.Color.Black);
System.Drawing.Graphics formGraphics = this.pictureBox1.CreateGraphics();
Point startPos = new Point(wps[0].X, wps[0].Y);
foreach (Point p in wps)
{
formGraphics.DrawLine(myPen, startPos.X, startPos.Y, p.X, p.Y);
startPos = p;
}
myPen.Dispose();
formGraphics.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
但没有得到任何东西!我对pictureBox的on_click事件做了同样的事情,但是如果循环一些Points我只使用鼠标X和Y.我已经验证了他们不包含rubish的点列表:D
在paint事件中编写代码,以便它可以引用.picturebox.Invalidate()将调用Paint().
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.AliceBlue);
PointF p = new PointF();
e.Graphics.DrawLine(pen,p.X,p.Y);
}
Run Code Online (Sandbox Code Playgroud)
它适合您的代码
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(System.Drawing.Color.Black);
foreach (List<Point> wps in map.waypoints)
{
Point startPos = new Point(wps[0].X, wps[0].Y);
foreach (Point p in wps)
{
e.Graphics.DrawLine(myPen, startPos.X, startPos.Y, p.X, p.Y);
startPos = p;
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果你想在某个函数中绘制线条说
public void DoFunction()
{
.....
.....
pictureBox1.Invalidate() /* here automatic call to pictureBox1_Paint(object sender, PaintEventArgs e) */
. . . .
}
Run Code Online (Sandbox Code Playgroud)
得到它了?
| 归档时间: |
|
| 查看次数: |
1242 次 |
| 最近记录: |