新建一个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”参数请问是哪里出了问题呢,谢谢!

解决方案 »

  1.   

    Graphics graph=new Graphics();
        graph=Graphics.FromHwnd(pictureBox1.Handle);
    改为
     Graphics graph=Graphics.FromHwnd(pictureBox1.Handle);
    Graphics没有构造函数
      

  2.   

    Graphics graph=Graphics.FromHwnd(pictureBox1.Handle);
    graph.FillRectangle(new SolidBrush(Color.Yellow),10,20,13,7);Graphics 没有构造函数。
      

  3.   

    应该说他的构造函数是private,无法在外部访问。
      

  4.   

    private?为什么这么说?msdn上看不到Graphics的构造函数
      

  5.   

    private void paint()
    {
        Graphics graph=Graphics.FromHwnd(pictureBox1.Handle);
        graph.FillRectangle(new SolidBrush(Color.Yellow),10,20,13,7);
    }
      

  6.   

    private void paint()
    {
        Graphics graph=Graphics.FromHwnd(pictureBox1.Handle);
        graph.FillRectangle(new SolidBrush(Color.Yellow),10,20,13,7);
    }
      

  7.   

    To deadshot123(随风缘) :abstract的类才"没有"构造函数。我们写类代码的时候如果不写构造函数,那么调用的时候编译也不会有错误。比如类:
    public class Class2
    {}
    引用:Class2 c = new Class2();也不会出错。这种情况,我们说Class2有没有构造函数?
    但我说的那个private应该也错了。刚才测试如果把构造函数写成private,那么调用的时候提示:不可访问“WindowsApplication1.Class2.Class2()”,因为它受保护级别限制
      

  8.   

    TO combread()
    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提供一个默认无参数的构造函数 因此这个是正确的当一个类有提供构造函数的时候 就不会调用默认的构造函数