使用类库创建饼状/条形图形
本文关键字:图形 类库 创建 | 更新日期: 2023-09-27 18:16:04
我想创建一个能够绘制饼状图或条形图的类库。我使用以下代码…
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);
}
}
}
和errors: Error 1 'KouChart。Pasta'不包含'CreateGraphics'的定义,也没有扩展方法'CreateGraphics'接受类型为'KouChart '的第一个参数。可以找到Pasta(您是否缺少使用指令或汇编参考?)c:' users ' muu ' documents ' visual Studio 2010'Projects'KouChart'KouChart'Pasta.cs 20 31 KouChart
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);
}
}
还请注意,缓存这些Pen
和Brush
实例比每次重新创建它们更有效。
this.CreateGraphics();表示在Pasta上存在一个名为CreateGraphics的方法。这种方法在哪里?它似乎完全消失了。我猜你正在使用的代码,预期(这)是一个控制或形式?也许传递一个对控件的引用并调用createGraphics ?
你能像
那样做吗?Graphics g = new Control().CreateGraphics();