你可以这样做,但你应该在Paint
方法中绘制所有对象:
public abstract class DrawingObject
{
public abstract void Draw(Graphics g);
}
public class Line : DrawingObject
{
public Point Start { get; set; }
public Point End { get; set; }
public float Thickness { get; set; }
public Color Color { get; set; }
public override void Draw(Graphics g)
{
g.DrawLine(new Pen(Color, Thickness), Start, End);
}
}
// Other classes derived from `DrawingObject`
在您的表单中,存储您DrawingObjects
的 s 列表并在需要时绘制它们:
public partial class MainForm : Form
{
//...
List<DrawingObject> _objects = new List<DrawingObject>();
private void PanelOne_Paint(object sender, PaintEventArgs e)
{
base.OnPaint(e);
foreach (var o in _objects)
{
o.Draw(e.Graphics);
}
}
}
Invalidate()
当您的对象发生任何更改(新行、删除行、移动行等)时调用面板的方法。
为避免绘图时闪烁,您可以将DoubleBuffer
面板的属性设置为true
。