新建一个Windows Form,然后添加一个Picture Box控件,希望通过FromHwnd方法来获得一个Graphics对象,然后在这个对象上画一个矩形。代码如下:##
...
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
## (Windows Form 和 添加控件时自动生成的代码,没有做任何改动)private void paint()
{
Graphics graph=new Graphics();
graph=Graphics.FromHwnd(pictureBox1.Handle);
graph.FillRectangle(new SolidBrush(Color.Yellow),10,20,13,7);
}paint方法即是我自己添加的代码,从pictureBox1获得Graphics对象,然后就在pictureBox1中绘图。但是调试时提示: 重载“Graphics”方法未获取“0”参数请问是哪里出了问题呢,谢谢!
...
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
## (Windows Form 和 添加控件时自动生成的代码,没有做任何改动)private void paint()
{
Graphics graph=new Graphics();
graph=Graphics.FromHwnd(pictureBox1.Handle);
graph.FillRectangle(new SolidBrush(Color.Yellow),10,20,13,7);
}paint方法即是我自己添加的代码,从pictureBox1获得Graphics对象,然后就在pictureBox1中绘图。但是调试时提示: 重载“Graphics”方法未获取“0”参数请问是哪里出了问题呢,谢谢!
graph=Graphics.FromHwnd(pictureBox1.Handle);
改为
Graphics graph=Graphics.FromHwnd(pictureBox1.Handle);
Graphics没有构造函数
graph.FillRectangle(new SolidBrush(Color.Yellow),10,20,13,7);Graphics 没有构造函数。
{
Graphics graph=Graphics.FromHwnd(pictureBox1.Handle);
graph.FillRectangle(new SolidBrush(Color.Yellow),10,20,13,7);
}
{
Graphics graph=Graphics.FromHwnd(pictureBox1.Handle);
graph.FillRectangle(new SolidBrush(Color.Yellow),10,20,13,7);
}
public class Class2
{}
引用:Class2 c = new Class2();也不会出错。这种情况,我们说Class2有没有构造函数?
但我说的那个private应该也错了。刚才测试如果把构造函数写成private,那么调用的时候提示:不可访问“WindowsApplication1.Class2.Class2()”,因为它受保护级别限制
Graphic的构造函数应该是private应该是对的Graphics是sealed类 不允许继承[ComVisible(false)]
public sealed class Graphics : MarshalByRefObject, IDisposable[Serializable]
public abstract class MarshalByRefObjectprotected MarshalByRefObject();从msdn上找到的原型因此Graphic的构造函数应该是privatepublic class Class2
{}
引用:Class2 c = new Class2();也不会出错。这种情况,我们说Class2有没有构造函数?这种是由于Class2()调用默认的构造函数System.Object提供一个默认无参数的构造函数 因此这个是正确的当一个类有提供构造函数的时候 就不会调用默认的构造函数