2011-06-10 54 views
0

我想用這種方法面板繪製:C#繪製面板中

protected override void InitOutput(object output) 
     { 
      if (output is Control) 
      { 
       Control c = (Control)output; 
       g.FillRectangle(hb, 7, 10, 30 - 19, 5); 
       ... 
      } 

用文本我可以這樣做:

protected override void InitOutput(object output) 
     { 
      if (output is Control) 
      { 
       Control c = (Control)output; 
       lbl.Name = "lbl"; 
       lbl.Size = new System.Drawing.Size(10, 10); 
       lbl.TabIndex = 5; 
       lbl.Text = "test"; 

       panel.Location = new System.Drawing.Point(1, 1); 
       panel.Name = "panelSys"; 
       panel.Size = new System.Drawing.Size(20, 20); 
       panel.TabIndex = 5; 
       panel.Controls.Add(lbl); 
       c.Controls.Add(panelSys); 
      } 

希望你能幫助我 感謝

+0

g從哪裏來的InitOutput方法?在繪畫事件之外拿着一個圖形對象可能不是最好的格式。嘗試在控件的Paint事件中完成所有繪畫。 – LarsTech 2011-06-10 18:21:49

回答

1

我不知道爲什麼你需要InitOtuput功能,但如果你想從中得出你可以做這樣的:

private void InitOutput(object output) 
{ 
    if (output is Control) 
    { 
     Control c = (Control)output; 
     c.Paint += new System.Windows.Forms.PaintEventHandler(c_Paint); 
     // Invalidate needed to rise paint event 
     c.Invalidate(); 
    } 
} 
private void c_Paint(object sender, PaintEventArgs e) 
{ 
    SolidBrush hb = new SolidBrush(Color.Red); 
    e.Graphics.FillRectangle(hb, 7, 10, 30 - 19, 5); 
    e.Graphics.DrawString("test", DefaultFont, hb, new PointF(50, 50)); 
} 

Additionaly你不需要使用標籤來繪製文本U可以使用Graphics.DrawSting繪製它

0

繪製到控件應該通過向控件添加一個'Paint'事件並在該事件中繪製。你將通過EventArgs得到一個'Graphics'對象。 然後,您可以使用控件的「無效」方法強制繪製控件。此外,Windows有時會自己調用Paint事件。

或者,您也可以使用'Bitmap.Create'創建一個normale。繪製到,然後將其分配給Picture控件。

+0

嘿,我必須創建一個繪畫事件,並在那裏是繪圖代碼(例如:g.DrawLine(...)),並在上面的繪製方法形式有無效? – inqui 2011-06-10 17:31:00

+0

當我回到我的Visual Studio PC時,我會爲你寫一些示例代碼,但它會和Archibald的代碼一樣。 – 2011-06-10 19:44:26