2011-11-28 64 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

。没有办法可以打电话,或者没有什么可以借鉴的。你可以随时创建一个控件吗? –