2013-06-29 130 views

回答

0

下面是一个将每个“形状”作为GraphicsPath存储在类级别List中的快速示例。每个路径都使用窗体的Paint()事件中提供的Graphics绘制。随机矩形添加到列表<>与每个按钮的点击和刷新()被调用针对的形式,迫使它重新绘制本身:

public partial class Form1 : Form 
{ 

    public Form1() 
    { 
     InitializeComponent(); 
     this.Paint += new PaintEventHandler(Form1_Paint); 
    } 

    private Random R = new Random(); 
    private List<System.Drawing.Drawing2D.GraphicsPath> Paths = new List<System.Drawing.Drawing2D.GraphicsPath>(); 

    private void button1_Click(object sender, EventArgs e) 
    { 
     Point pt1 = new Point(R.Next(this.Width), R.Next(this.Height)); 
     Point pt2 = new Point(R.Next(this.Width), R.Next(this.Height)); 

     System.Drawing.Drawing2D.GraphicsPath shape = new System.Drawing.Drawing2D.GraphicsPath(); 
     shape.AddRectangle(new Rectangle(new Point(Math.Min(pt1.X,pt2.X), Math.Min(pt1.Y, pt2.Y)), new Size(Math.Abs(pt2.X - pt1.X), Math.Abs(pt2.Y - pt1.Y)))); 
     Paths.Add(shape); 

     this.Refresh(); 
    } 

    private void Form1_Paint(object sender, PaintEventArgs e) 
    { 
     Graphics G = e.Graphics; 
     foreach (System.Drawing.Drawing2D.GraphicsPath Path in Paths) 
     { 
      G.DrawPath(Pens.Black, Path); 
     } 
    } 

} 
0

为了提高甚至用手阅读本SO后的涂料(基本调用invalidate()方法)

SO post: How do I call paint event?

然而,你可能需要有某种形式的内部“drawshape”标志,设置/清除按钮点击查看您的油漆,甚至处理方法中。这个标志将会调用你的绘画事件处理程序来继续绘制你的形状或者根本不绘制你的形状(每次调用表单绘制时)