长风秋雁
虽然代码没有语法错误,但却达不到你要的效果。g.RrawRectangle(……)应该放在Form1的Paint事件处理函数中!public partial class Form1 : Form{ Graphics g; Rectangle rect public Form1() { InitializeComponent(); g = this.CreateGraphics(); rect = new Rectangle(5, 10, 30, 30); } private void Form1_Paint(object sender, PaintEventArgs e) { // 作图方法必须在Paint事件中处理! g.DrawRectangle(Pens.Red, rect); }}作图方法必须在Paint事件中处理。原因与Form的刷新有关:每当窗体被Windows系统刷新都会引发Paint事件。在Paint事件中,你必须重新绘制你要的图形,否则画出的图形在窗体被刷新后就消失了;如果你把绘制图形的方法放到别的地方(比如你放在Form的Load事件处理中),窗体被Windows刷新后,你画出的矩形就被刷没了。另,上面的代码可以简化为:public partial class Form1 : Form{ //Graphics g; Rectangle rect public Form1() { InitializeComponent(); //g = this.CreateGraphics(); rect = new Rectangle(5, 10, 30, 30); } private void Form1_Paint(object sender, PaintEventArgs e) { //直接使用事件参数e中的Graphics作图 e.Graphics.DrawRectangle(Pens.Red, rect); }}