当前位置:编程学习 > C#/ASP.NET >>

Graphics g = this.CreateGraphics(); 的意思?

Graphics g = this.CreateGraphics(); Graphics 这个类创建对象怎么会是这样的格式呢?Graphics g=new Graphics ();一般不都是这样的吗?如果说 Graphics g = this.CreateGraphics(); 左边是创建对象那么右边又是什么? Graphics g = --------------------编程问答-------------------- Graphics g=new Graphics ();
你用g画,在哪个画布上画呢?

Graphics g = this.CreateGraphics();明确了g是在this上画(如果this是当前窗体,则在窗体上画;如果this是PictureBox,则在pictureBox上画)

还可以是这样的定义
Graphics g = pictureBox1.CreateGraphics();则g在pictureBox1上画 --------------------编程问答--------------------
引用 1 楼 gxingmin 的回复:
Graphics g=new Graphics ();
你用g画,在哪个画布上画呢?

Graphics g = this.CreateGraphics();明确了g是在this上画(如果this是当前窗体,则在窗体上画;如果this是PictureBox,则在pictureBox上画)

还可以是这样的定义
Graphics g = pictureBox1.……
解释得灰常清楚 --------------------编程问答-------------------- 我思想短路了吧, --------------------编程问答--------------------  Graphics g = this.CreateGraphics(); 左边是创建对象那么右边又是什么?
左边是声明,右边才是创建对象,this.CreateGraphics()表示创建当前窗体的画布 --------------------编程问答-------------------- 是不是也就是说Graphics这个类比较特殊,不能用new创建? --------------------编程问答--------------------
引用 5 楼 Jason_Song1109 的回复:
是不是也就是说Graphics这个类比较特殊,不能用new创建?


Graphics这个类,设计成依赖作用对象,故意防止直接调用构造来创建对象 --------------------编程问答-------------------- Graphics本身就没有可访问的构造函数,lz确认你在vs里能定义 Graphics g= new Graphics(); ?不报错吗? --------------------编程问答-------------------- 楼上很详细。 --------------------编程问答-------------------- Graphics g = this.CreateGraphics(); 
左边是定义一个Graphics类型的变量
--------------------编程问答--------------------
引用 1 楼 gxingmin 的回复:
Graphics g=new Graphics ();
你用g画,在哪个画布上画呢?

Graphics g = this.CreateGraphics();明确了g是在this上画(如果this是当前窗体,则在窗体上画;如果this是PictureBox,则在pictureBox上画)

还可以是这样的定义
Graphics g = pictureBox1.……


就是这个意思 --------------------编程问答-------------------- 如何:创建用于绘制的 Graphics 对象?
1、获取对 Paint 事件的 PaintEventArgs 中 Graphics 对象的引用
   private void Form1_Paint(object sender, 
        System.Windows.Forms.PaintEventArgs pe) 
   {
   // Declares the Graphics object and sets it to the Graphics object
   // supplied in the PaintEventArgs.
   Graphics g = pe.Graphics;
   // Insert code to paint the form here.
  }

2、CreateGraphics()方法
  也可以使用控件或窗体的 CreateGraphics 方法来获取对 Graphics 对象的引用,该对象表示该控件或窗体的绘 图图面。 
   Graphics g;
   // Sets g to a graphics object representing the drawing surface of the
   // control or form g is a member of.
   g = this.CreateGraphics();

3、从 Image 创建 Graphics 对象
   Bitmap myBitmap = new Bitmap(@"C:\Documents and 
   Settings\Joe\Pics\myPic.bmp");
   Graphics g = Graphics.FromImage(myBitmap);

参考msdn,自己可以去看看,上面解释得很详细:
http://msdn.microsoft.com/zh-cn/library/5y289054.aspx


--------------------编程问答-------------------- 为什么我用 Graphics* g = this->CreateGraphics();

结果:error C2039: “CreateGraphics”: 不是“Ctest14Dlg”的成员 c:\users\brandy\documents\visual studio 2010\projects\test14\test14\test14dlg.cpp

我是建立的基于对话框的MFC应用程序
--------------------编程问答-------------------- 为什么我用 Graphics* g = this->CreateGraphics();

结果:error C2039: “CreateGraphics”: 不是“Ctest14Dlg”的成员 c:\users\brandy\documents\visual studio 2010\projects\test14\test14\test14dlg.cpp

我是建立的基于对话框的MFC应用程序 --------------------编程问答--------------------
引用 2 楼 yangjjfan 的回复:
Quote: 引用 1 楼 gxingmin 的回复:

Graphics g=new Graphics ();
你用g画,在哪个画布上画呢?

Graphics g = this.CreateGraphics();明确了g是在this上画(如果this是当前窗体,则在窗体上画;如果this是PictureBox,则在pictureBox上画)

还可以是这样的定义
Graphics g = pictureBox1.……
解释得灰常清楚
为什么我用 Graphics* g = this->CreateGraphics();

结果:error C2039: “CreateGraphics”: 不是“Ctest14Dlg”的成员 c:\users\brandy\documents\visual studio 2010\projects\test14\test14\test14dlg.cpp

我是建立的基于对话框的MFC应用程序 --------------------编程问答--------------------
引用 1 楼 gxingmin 的回复:
Graphics g=new Graphics ();
你用g画,在哪个画布上画呢?

Graphics g = this.CreateGraphics();明确了g是在this上画(如果this是当前窗体,则在窗体上画;如果this是PictureBox,则在pictureBox上画)

还可以是这样的定义
Graphics g = pictureBox1.CreateGraphics();则g在pictureBox1上画

有一事不明:
   this.CreateGraphics()这个方法是不是每次调用都会给出不同的Graphics对象,还是说每次都是给的基于this的某个属性呢? --------------------编程问答-------------------- 貌似在winform里面可以用this.graphics,在WPF里面就只能用Graphics.FromImage了.
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,