Graphics DrawLine

ouyang4683 2008-09-09 10:00:13
Pen p = new Pen(Color.Red, 1);
Point p1=...;
Point p2=...;
Graphics gr;
gr.DrawLine(p,p1,p2);

为什么总提示:使用了未赋值的局部变量“gr”


我用了gr=this.CreateGraphics();

但会要求创建:

private Graphics CreateGraphics()
{
throw new Exception("The method or operation is not implemented.");
}


难道因为我重写了redraw?

我现在就想画这个线,怎么画?
...全文
330 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaolongma007 2011-09-21
  • 打赏
  • 举报
回复
gr对象是未经初始化的,所以系统编译会提示那个错误。要解决这个编译错误很简单:
Graphics gr = null;
但很显然这并不是你的本意,很有可能你是要在某个窗口上绘制,或者在某个自己的内存中Grphics,因此问题的关键是解决这个Graphics对象的值的问题。
可能的做法是:
一、直接从某个窗口得到(this是窗口)
Graphics g =this.CreateGraphics();
g.Dispose()
二、从某个Graphics对象得到一个复制品(做双缓冲用之类的)
BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current;
BufferedGraphics bgr = currentContext.Allocate(SomeGraphics, SomeClipRectangle);
Graphics gr= bgr.Graphics;
这样就得到一个以SomeGraphics以基准和一个绘制对象了。


另外你使用这个方法的上下文环境可能提供了这样的参数,比如在OnPaint方法调用此函数时,OnPait带Graphics参数。

具体的Graphics对象的获取可参考一下这里:
http://blog.programfan.com/article.asp?id=17249
net0003 2008-09-09
  • 打赏
  • 举报
回复
o
ouyang4683 2008-09-09
  • 打赏
  • 举报
回复
...

窗体方法a 调用类里方法b 类里方法b调用类里方法c 类里方法c算出这2个点

现在就是 类里方法C 没直接画出这个线
zzyhuian06142 2008-09-09
  • 打赏
  • 举报
回复
那就无能为力了,不知道你在说什么了
ouyang4683 2008-09-09
  • 打赏
  • 举报
回复
在窗体上直接画也不太可能...

我得不到那2个点point
ouyang4683 2008-09-09
  • 打赏
  • 举报
回复
不是啊

在窗体上调用了一个方法a,

a又调用了方法b,b又调用了c ...

反正,最后才到这个函数,

用全局变量返回值,我觉得不可行...

zzyhuian06142 2008-09-09
  • 打赏
  • 举报
回复
那你就
Form2 f2 = new Form2();
f2.Show();
f2.CreateGraphics().DrawLine(new Pen(Color.Red), 100, 5, 10, 100);
ouyang4683 2008-09-09
  • 打赏
  • 举报
回复
不能在类里直接画么

因为现在的点都是写的常数,

真正在程序里中个参数啊...

就是DrawLine(pen ,point ,point)

后两个变量这不是还得建全局的传回来,多麻烦....
zzyhuian06142 2008-09-09
  • 打赏
  • 举报
回复
你先在窗体里面定义一个public Graphics g=null;
如果在Class里面
Form1 f2 = new Form1();
f2.Show();
f2.g = f2.CreateGraphics();
f2.g.DrawLine(new Pen(Color.Red), 100, 5, 10, 100);
ouyang4683 2008-09-09
  • 打赏
  • 举报
回复
用了9楼那个http://blog.programfan.com/article.asp?id=17249

窗体:
private void button4_Click(object sender, EventArgs e)
{
Class1 ab=new Class1();
ab.a();
}

Class:

public class Class1
{
public void a()
{
Graphics gr;
Pen myPen = new Pen(Color.Red, 2);
Form1 s = new Form1();
gr = Graphics.FromHwnd(s.Handle);
gr.DrawLine(myPen, 30, 60, 150, 60);

}
}

画不出来...

优途科技 2008-09-09
  • 打赏
  • 举报
回复
Graphics对象是你绘图的“上下文”对象,它表示了你绘制的图形将在哪个窗口的哪些区域。
你的代码中:
Pen p = new Pen(Color.Red, 1);
Point p1=...;
Point p2=...;
Graphics gr;
gr.DrawLine(p,p1,p2);

gr对象是未经初始化的,所以系统编译会提示那个错误。要解决这个编译错误很简单:
Graphics gr = null;
但很显然这并不是你的本意,很有可能你是要在某个窗口上绘制,或者在某个自己的内存中Grphics,因此问题的关键是解决这个Graphics对象的值的问题。
可能的做法是:
一、直接从某个窗口得到(this是窗口)
Graphics g =this.CreateGraphics();
g.Dispose()
二、从某个Graphics对象得到一个复制品(做双缓冲用之类的)
BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current;
BufferedGraphics bgr = currentContext.Allocate(SomeGraphics, SomeClipRectangle);
Graphics gr= bgr.Graphics;
这样就得到一个以SomeGraphics以基准和一个绘制对象了。


另外你使用这个方法的上下文环境可能提供了这样的参数,比如在OnPaint方法调用此函数时,OnPait带Graphics参数。

具体的Graphics对象的获取可参考一下这里:
http://blog.programfan.com/article.asp?id=17249
zhgroup 2008-09-09
  • 打赏
  • 举报
回复
你可以这样,从UserControl继承,然后重写OnPaint事件,该事件的e.Graphics就是Graphics对象
另外,你可以到codeproject上搜索一下DrawTools,这个例子就是将绘图集成到一个控件中
zzyhuian06142 2008-09-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ouyang4683 的回复:]
当前确实只是个类...

我要怎么画到别的窗体上?
[/Quote]
Form2 f2 = new Form2();
IntPtr i = f2.Handle;
Graphics g = Graphics.FromHwnd(i);
Pen p = new Pen(Color.Red, 1);
Point p1=...;
Point p2=...;
g.DrawLine(p,p1,p2);
huangma1616 2008-09-09
  • 打赏
  • 举报
回复
public class DrawLine
{
public void Draw(Graphics g)
{

Pen pen = new Pen(Color, PenWidth);

g.DrawLine(pen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);

pen.Dispose();
}
}

窗体:
Graphics gr=this.CreateGraphics();
new DrawLine().Draw(gr);
wdgphc 2008-09-09
  • 打赏
  • 举报
回复
Graphics gr;
gr是你要画图的落笔点,现在你没有定义,所以报错.
比如你要画在窗体上,还是某个bmp上,或者别的应用程序上,或者桌面上等等,你必须指定一个地方.(返回一个句柄)
brallow 2008-09-09
  • 打赏
  • 举报
回复
Graphics对象是你绘图的“上下文”对象,它表示了你绘制的图形将在哪个窗口的哪些区域。
你的代码中:
Pen p = new Pen(Color.Red, 1);
Point p1=...;
Point p2=...;
Graphics gr;
gr.DrawLine(p,p1,p2);

gr对象是未经初始化的,所以系统编译会提示那个错误。要解决这个编译错误很简单:
Graphics gr = null;
但很显然这并不是你的本意,很有可能你是要在某个窗口上绘制,或者在某个自己的内存中Grphics,因此问题的关键是解决这个Graphics对象的值的问题。
可能的做法是:
一、直接从某个窗口得到(this是窗口)
Graphics g =this.CreateGraphics();
g.Dispose()
二、从某个Graphics对象得到一个复制品(做双缓冲用之类的)
BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current;
BufferedGraphics bgr = currentContext.Allocate(SomeGraphics, SomeClipRectangle);
Graphics gr= bgr.Graphics;
这样就得到一个以SomeGraphics以基准和一个绘制对象了。


另外你使用这个方法的上下文环境可能提供了这样的参数,比如在OnPaint方法调用此函数时,OnPait带Graphics参数。

具体的Graphics对象的获取可参考一下这里:
http://blog.programfan.com/article.asp?id=17249
ouyang4683 2008-09-09
  • 打赏
  • 举报
回复
当前确实只是个类...

我要怎么画到别的窗体上?
slimfeng 2008-09-09
  • 打赏
  • 举报
回复

Graphics gr;
gr.DrawLine(p,p1,p2);

//为什么总提示:使用了未赋值的局部变量“gr”

//因为你没有创建对象graphics实例对象,加上gr=this.CreateGraphics();肯定没有问题

ouyang4683 2008-09-09
  • 打赏
  • 举报
回复
如果好用,我就不用提问是吧...
用了:gr=this.CreateGraphics();
会要求我重写: private Graphics CreateGraphics()

这就是不是系统里自带那个函数了...

这个线我就没画出来
zhgroup 2008-09-09
  • 打赏
  • 举报
回复
首先要确认你想在什么上画线,
可以使用Graphics.FromHwnd,Graphics.FromHdc,Graphics.FromImage方法创建Graphics对象,然后绘制
加载更多回复(5)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace DoubleBufferDraw { public partial class DrawLine : Form { class LineObj { private Point m_start; private Point m_end; public LineObj(Point start, Point end) { this.m_start = start; this.m_end = end; } public void Draw(Graphics g, Pen pen) { g.DrawLine(pen, m_start, m_end); } } private Point m_startPoint = Point.Empty; List lineList = new List(); public DrawLine() { InitializeComponent(); } private void drawLine(Graphics graphics, Point startPoint, Point endPoint) { BufferedGraphicsContext context = BufferedGraphicsManager.Current; BufferedGraphics bg = context.Allocate(graphics, this.ClientRectangle); bg.Graphics.Clear(this.BackColor); foreach (LineObj line in this.lineList) { line.Draw(bg.Graphics, SystemPens.ControlText); } bg.Graphics.DrawLine(SystemPens.ControlText, startPoint, endPoint); bg.Render(); bg.Dispose(); bg = null; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); foreach (LineObj line in this.lineList) { line.Draw(e.Graphics, SystemPens.ControlText); } } protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); this.m_startPoint = new Point(e.X, e.Y); } protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (e.Button == MouseButtons.Left) { this.drawLine(this.CreateGraphics(), this.m_startPoint, new Point(e.X, e.Y)); } } protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); LineObj line = new LineObj(this.m_startPoint, e.Location); this.lineList.Add(line); } } }

110,552

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧