0
     static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new SpaceInvaders());// i get an error here when i start the form application.. it says Argument Exception. Parameter is not valid
    }

私のメインフォームは次のようになります。

    public SpaceInvaders()
    {
        InitializeComponent();

    }

    public void SpaceInvaders_Load(object sender, EventArgs e)
    {
}

ここにスタックトレースがあります

" System.Drawing.Graphics.GetHdc() で\r\n System.Drawing.BufferedGraphics.RenderInternal(HandleRef refTargetDC, BufferedGraphics buffer) で\r\n System.Drawing.BufferedGraphics.Render() で\r\n システムで.Windows.Forms.Control.WmPaint(Message& m)\r\n at System.Windows.Forms.Control.WndProc(Message& m)\r\n at System.Windows.Forms.ScrollableControl.WndProc(Message& m)\r \n System.Windows.Forms.ContainerControl.WndProc(Message& m)\r\n System.Windows.Forms.Form.WndProc(Message& m)\r\n System.Windows.Forms.Control.ControlNativeWindow.OnMessage (Message& m)\r\n at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)\r\n at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam )\r\n System.Windows.Forms.UnsafeNativeMethods.System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) で \r\n System.Windows で DispatchMessageW(MSG& msg)\r\n。 Forms.Application.ThreadContext.RunMessageLoopInner(Int32 理由、ApplicationContext コンテキスト)\r\n で System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 理由、ApplicationContext コンテキスト)\r\n で System.Windows.Forms.Application. Run(Form mainForm)\r\n D:\Documents and Settings\Dima\My Documents\Visual Studio 2008\Projects\SpaceInvaders\SpaceInvaders\Program.cs:line 18 の WindowsFormsApplication1.Program.Main() で\r\n System.AppDomain._nExecuteAssembly で (アセンブリ アセンブリ、String[] args)\r\n System.AppDomain.ExecuteAssembly で (String assemblyFile、Evidence assemblySecurity、String[] args)\r\n Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() で\r\n System.Threading.ThreadHelper.ThreadStart_Context(Object state) で\r\n System.Threading.ExecutionContext.Run( ExecutionContext executionContext、ContextCallback コールバック、オブジェクト状態)\r\n at System.Threading.ThreadHelper.ThreadStart()"

「パラメータが無効です。」

私は問題を見つけたと思います:

  public void Draw(Graphics  g,int animationCell)
    {
     // some code
        g.dispose()//that method threw the exception can someone tell me why? i thought you do need to dispose your graphics object at the end when you finish using it.
       }
4

3 に答える 3

2

SpaceInvadersフォーム内に移動して、メソッドにnullまたはその他の無効な引数が指定されていないかどうかを確認できるように、Step Into(F11)を使用してデバッグする必要があると思います。

この行では、例外がすぐにスローされない場合があります。

Application.Run(new SpaceInvaders());

ただし、一部の初期化関数が問題を引き起こしている可能性があります。

スタックトレースとコードを確認してから編集します。

plsはセクションを参照してください//some code。その前のコードg.dispose()が、Drawメソッドの後に実行されるイベントハンドラーを起動しないかどうかを確認します。もしそうなら、そのイベントハンドラーはグラフィックオブジェクトを必要とするものでなければなりませんが、uはすでにそれを破棄しています。したがって、グラフィックパラメータはnullです。//some codeさらにサポートが必要な場合は、その部分を入れてください。

于 2011-04-14T14:48:31.967 に答える
1

簡潔な答え:

Draw() メソッドの Graphics オブジェクトで Dispose() を呼び出さないでください。Graphics-object には、ネイティブ描画サーフェイスへのハンドルが含まれています。破棄すると、フォームを画面に描画できなくなります。

于 2011-04-14T15:49:19.237 に答える
1

他人のGraphics物を処分してはいけません。

自分が所有するオブジェクトのみを破棄し、他の誰もそれを使用しないことが確実な場合にのみ破棄してください。

他の人から使い捨てのオブジェクトを取得した場合は、(別の言い方をしない限り) 終了時に破棄されると想定する必要があります。あなたはそれを自分で処分すべきではありません。

于 2011-04-14T17:22:12.173 に答える