2016-04-15 45 views
-1
 private void DrawIt() 
     { 
      System.Drawing.Graphics graphics = this.CreateGraphics(); 
      System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(
       50, 50, 150, 150); 

      graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle); 
     } 
     private void Form1_Load(object sender, EventArgs e) 
     { 
      DrawIt(); 

     } 


     private void button1_Click(object sender, EventArgs e) 
     { 
      DrawIt(); 

     } 

將'DrawIt'方法放在按鈕事件中時它工作,但是在表單加載事件中它沒有,爲什麼?繪圖圖形無法工作表格加載事件

+1

嘗試將DrawIt()方法添加到Form_Shown()事件 – VDN

+3

擺脫CreateGraphics並使用OnPaint覆蓋。 – LarsTech

+0

@LarsTech這種方式太先進,即時通訊初學者:P – virtualmind

回答

0

將加載事件更改爲Paint。 如果你想重繪你的表單使用this.Refresh();

當你在Paint方法使用:在繪製表格前運行

private void mForm_Paint(object sender, PaintEventArgs e) 
{ 
    e.Graphics.FillEllipse(...); 
} 
0

Load事件。所以你繪製的任何東西都被Form所覆蓋。

從表單加載後觸發事件調用您的DrawIt方法。