获取System.ArgumentException

时间:2013-01-04 16:06:38

标签: c# winforms paint

我正在尝试制作一个简单的游戏,但没有遇到这个例外。

System.ArgumentException: Parameter is not valid.
   at System.Drawing.Graphics.GetHdc()
   at System.Drawing.BufferedGraphics.RenderInternal(HandleRef refTargetDC, BufferedGraphics buffer)
   at System.Drawing.BufferedGraphics.Render()
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at GECS.Core.Game.runGame(Game g) in c:\Users\sriharshachilakapati\Documents\SharpDevelop Projects\GECS\GECS\Core\Game.cs:line 16
   at GECS.Test.GameTest.Main(String[] args) in c:\Users\sriharshachilakapati\Documents\SharpDevelop Projects\GECS\GECS\Test\GameTest.cs

这是我使用的代码。

public class Game : Form {

    public Game() : base() {
        gameLoop();
    }

    public static void runGame(Game g){
        Application.Run(g);
    }

    public virtual void initResources(){}

    public virtual void update(long elapsedTime){}

    public virtual void render(Graphics g){}

    private void gameLoop(){
        DoubleBuffered = true;
/** The following line gives me exception **/
        Paint += delegate(object sender, PaintEventArgs e) { render(e.Graphics); };
        initResources();
        int now = getCurrentTime();
        int gameTime = getCurrentTime();
        long elapsedTime = 1000 / Global.STEPS_PER_SECOND;
        while (this.Created){
            this.Text = Global.TITLE;
            now = getCurrentTime();
            while (now>gameTime){
                update(elapsedTime);
                gameTime += (int)elapsedTime;
            }
            Refresh();
        }
    }

    public static int getCurrentTime(){
        return System.Environment.TickCount;
    }

}

感谢。

1 个答案:

答案 0 :(得分:0)

找到解决方案。这是因为我在调用Application.Run之前调用了循环。