2011-11-28 44 views
-1

我想創建一個能夠繪製餅圖或條形圖的類庫。 我使用如下代碼...使用類庫創建餅圖/條形圖

Graphics g = CreateGraphics(); 

當我使用該代碼的Visual Studio告訴我,你不能用dll文件(類庫)使用。

我還有問題,我怎樣才能解決這個問題?O_O

詢問詳細信息:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Drawing; 
using System.Data; 


namespace KouChart 
{ 
    public class Pasta 
    { 
     public void PastaCiz(int a, int b, int c) 
     { 
      float toplam = a + b + c; 
      float deg1 = (a/toplam) * 360; 
      float deg2 = (b/toplam) * 360; 
      float deg3 = (c/toplam) * 360; 
      Pen p = new Pen(Color.Red, 2); 
      Graphics g = this.CreateGraphics(); 
      Rectangle rec = new Rectangle(50, 12, 150, 150); 
      Brush b1 = new SolidBrush(Color.Red); 
      Brush b2 = new SolidBrush(Color.Black); 
      Brush b3 = new SolidBrush(Color.Blue); 
      Brush b4 = new SolidBrush(Color.Yellow); 

      g.DrawPie(p, rec, 0, deg1); 
      g.FillPie(b1, rec, 0, deg1); 
      g.DrawPie(p, rec, deg1, deg2); 
      g.FillPie(b2, rec, deg1, deg2); 
      g.DrawPie(p, rec, deg2 + deg1, deg3); 
      g.FillPie(b3, rec, deg2 + deg1, deg3); 
     } 
    } 
} 

和錯誤:錯誤1「KouChart.Pasta」不包含'的定義CreateGraphics「,並且沒有擴展方法'CreateGraphics'接受類型'KouChart.Pasta'的第一個參數可以找到(你是否缺少使用指令或程序集引用?)C:\ Users \ Muyu \ Documents \ Visual Studio 2010 \ Projects \ KouChart \ KouChart \ Pasta.cs 20 31 KouChart

+1

你能發佈實際的錯誤信息嗎? –

+0

請向我們展示更多代碼和完整的錯誤細節,您當前的描述有點過於籠統;-) –

+0

對不起,我發佈了更多信息。 – TheMuyu

回答

1

CreateGraphics()方法屬於Control類。如果Pasta應該是一個控件,那麼您應該從Control中獲取它。

public class Pasta : Control 
{ 
    public void PastaCiz(int a, int b, int c)   
    { ... } 
} 

順便說一句,如果你正在編寫一個控制,你想繪製在OnPaint()方法,而你並不需要調用CreateGraphics(),因爲一個已經創建爲你。這裏有一個非常快速的例子來說明,但我不是一個控制開發人員,所以請不要認爲這是正確的方法。

public class Pasta : Control 
{ 
    int a; 
    int b; 
    int c; 

    public void PastaCiz(int a, int b, int c) 
    { 
     this.a = a; 
     this.b = b; 
     this.c = c; 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 
     float toplam = a + b + c; 
     float deg1 = (a/toplam) * 360; 
     float deg2 = (b/toplam) * 360; 
     float deg3 = (c/toplam) * 360; 
     Pen p = new Pen(Color.Red, 2); 
     Graphics g = e.Graphics;       <-- note 
     Rectangle rec = new Rectangle(50, 12, 150, 150); 
     Brush b1 = new SolidBrush(Color.Red); 
     Brush b2 = new SolidBrush(Color.Black); 
     Brush b3 = new SolidBrush(Color.Blue); 
     Brush b4 = new SolidBrush(Color.Yellow); 
     g.DrawPie(p, rec, 0, deg1); 
     g.FillPie(b1, rec, 0, deg1); 
     g.DrawPie(p, rec, deg1, deg2); 
     g.FillPie(b2, rec, deg1, deg2); 
     g.DrawPie(p, rec, deg2 + deg1, deg3); 
     g.FillPie(b3, rec, deg2 + deg1, deg3); 
    } 
} 

另外請注意,這將是更高效的高速緩存那些PenBrush實例,而不是每次都重新創建它們。

+0

我正在嘗試使用您的代碼。但有錯誤或我錯過了一些東西..這裏是截圖:http://a1111.hizliresim.com/r/w/t88q.png – TheMuyu

+0

你需要把'使用System.Windows.Forms;'在文件的頂部。 –

+0

哈哈,這不是winforms。這是類庫。我不需要使用勝利形式。順便說一下,我解決了問題thnx幫助 – TheMuyu

1

th is.CreateGraphics();表明麪食上會存在名爲CreateGraphics的方法。這種方法在哪裏?它似乎完全缺失。我猜你正在使用的代碼,預計(這)是一個控制或形式?也許傳遞一個控件的參考並調用createGraphics?

,你可以這樣做

Graphics g = new Control().CreateGraphics(); 
+0

CreateGraphics屬於System.Drawing,當我使用winforms沒有錯誤。但是當我使用ClassLibrary時會出現這個錯誤。 – TheMuyu

+0

。沒有辦法可以打電話,或者沒有什麼可以借鑑的。你可以隨時創建一個控件嗎? –