绘制矩形不起作用

时间:2014-03-14 11:12:32

标签: c# forms graphics drawing shape

你能解释一下这段代码有什么问题吗?因为它没有画任何东西。 它不是假设在我的表格中画一个矩形?谢谢!

  public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        Graphics g = this.CreateGraphics();
        Rectangle r = new Rectangle(0, 0, 150, 150);
        g.DrawRectangle(System.Drawing.Pens.Black, r);
    }
}

1 个答案:

答案 0 :(得分:2)

使用OnPaint方法进行绘画:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    Graphics g = e.Graphics;
    Rectangle r = new Rectangle(0, 0, 150, 150);
    g.DrawRectangle(System.Drawing.Pens.Black, r);
}