C#中Graphics的应用?

public void DrawMe() //这里该写什么???
{
SolidBrush blueBrush = new SolidBrush(Color.Blue);

// Create location and size of rectangle.
int x = 0;
int y = 0;
int width = 200;
int height = 200;

// Fill rectangle to screen.

e.Graphics.FillRectangle(blueBrush, x, y, width, height);//这里会提示错误,说Graphics引用不到,为什么啊?

System.Diagnostics.Trace.TraceInformation("Lei's DrawMe");
}
有什么要注意的?为什么引用了using System.Drawing; 命名空间还是提示错误?
在其他类里还有一个调用这个方法的地方,代码如下:
private void panel1_Paint(object sender, PaintEventArgs e)
{
controller.DrawEntity();//这个括号里应该写什么呢?编译时这里会提示错误?

}
那位高手能指点一下呢!谢了?

在Paint事件中,Paint的成员PaintEventArgs类有Graphics属性,e是PaintEventArgs类的对象。用对象e调用Graphics属性。你的DrawMe方法中没有输入参数PaintEventArgs e,应当用CreateGraphics方法创建图形对象:
Graphics g = this.CreateGraphics(); //窗体获取Graphics对象的引用
Graphics g = control.CreateGraphics(); //控件获取Graphics对象的引用
你这里大概是窗体,因此你的代码改为:
Graphics g = this.CreateGraphics();
g.FillRectangle(blueBrush, x, y, width, height);
就可以了。
如果你想要在DrawMe中添加参数e为图形对象,DrawMe应当是某个事件(通常是Paint事件)的处理程序,要声明一个委托和事件,构造事件处理方法,这个事件处理方法是根据你的委托和事件名称,当你敲+=时系统提示你按Tap键,自动给你加上的,并且参数也是系统给你加上的,一般不自己做,在属性框选择合适的事件。
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-04-07
private void panel1_Paint(object sender, PaintEventArgs e)
{
DrawMe(e.Graphics);
}

private void DrawMe(Graphics g){

g.FillRectangle(blueBrush, x, y, width, height);...
这里绘制
主意:绘制结束后不要使用 g.Dispone() 因为这个g来自控件绘制本身 不能销毁

}本回答被提问者采纳
第2个回答  2011-04-08
你是想在panel1_Paint事件中调用 DrawMe函数吗?
把panel1_Paint事件的参数传进去就可以了:
private void panel1_Paint(object sender, PaintEventArgs e)
{
DrawMe(e) ;
}
当然,你的DrawMe方法也要改写,加一个参数
public void DrawMe(PaintEventArgs e) ;
第3个回答  2011-04-07
e.Graphics.FillRectangle(blueBrush, x, y, width, height);
这个e从哪里来的。一般是要在onpaint事件中处理的。追问

在定义DrawMe() 方法时,有个参数:(Graphics e)但是加上了又会有下面的错误:错误 4 “System.Drawing.Graphics”不包含“Graphics”的定义,并且找不到可接受类型为“System.Drawing.Graphics”的第一个参数的扩展方法“Graphics”(是否缺少 using 指令或程序集引用?)
这是什么原因啊?
在前面有掉用这个方法的地方,里面需要有参数值,但是我不知道该怎么写!谢了

追答

这个是要在哪里画图?这个代码不太全啊,看不到是怎么调用的。

相似回答
大家正在搜