使用c#在Direct3D中CTRL ALT DEL时出现DeviceLostException

本文关键字:DEL DeviceLostException ALT CTRL Direct3D 使用 | 更新日期: 2023-09-27 17:53:44

我用的是微软。DirectX和微软的DirectX。Direct3D参考做一些绘图在我的形式。当我运行程序时,用户在Windows XP中按CTRL ALT DEL并弹出"Windows安全"窗体,当返回到窗体时,抛出DeviceLostException,当试图处理此异常时,似乎没有办法让它回来。

我对这个问题做了一些研究,并尝试了几种编码解决方案。

        try
        {
             _d3ddevice.Present();                 
        }
        catch 
        {
            DeviceLost = true;
        }
        if (DeviceLost) 
        {
            AttemptRecovery();
        }
        this.Invalidate();
        ReadKeyboard();
        base.OnPaint(e);          
    }    
    private void AttemptRecovery()
    {
        try
        {
            _d3ddevice.TestCooperativeLevel();
        }
        catch (DeviceLostException)
        {
            Application.Exit();
        }
        catch (DeviceNotResetException)
        {
            try
            {
                _d3ddevice.Reset(_params);
                DeviceLost = false;
                InitGraphics();
                CameraPositioning();
                VertexDeclaration();
                IndicesDeclaration();         
            }
            catch (DeviceLostException)
            {
            }
        }
    }

当程序调用testcooperativellevel()时,如果再次捕获DeviceLostException,则表示没有必要尝试重置设备。

如何重置设备并继续在窗体中绘图?

使用c#在Direct3D中CTRL ALT DEL时出现DeviceLostException

4分两句话:

    你不应该用微软。DirectX很早以前就被弃用了。可以用SlimDX或SharpDX代替。
  • 为了重新创建您的设备,您首先必须等待,直到设备可以恢复
  • 当设备可以恢复时,你必须释放所有的显存对象,并重新创建它们。
  • 调用Reset方法。