Option Strict On Option Explicit On Imports System.IO Imports System.Drawing Imports Microsoft.DirectX Imports Microsoft.DirectX.Direct3D Public Class frmMain Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Ending As Boolean Shared Sub Main() Dim Frm As New frmMain Dim GCard As New DXDevice(Frm.Handle, True) GCard.LoadSprite("sprite.bmp") Frm.Show() Try Do GCard.Render() Application.DoEvents() Loop Until Frm.Ending Debug.WriteLine("Escape triggered in main sub") GCard = Nothing Frm.Dispose() Catch ex As Exception Debug.WriteLine("Error occurred in main sub - " & ex.ToString) End Try End Sub Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown If e.KeyCode = Keys.Escape Then 'Close the window when the user presses escape Debug.WriteLine("Escape triggered by Escape Key") Ending = True End If End Sub End Class Public Class DXDevice Private m_device As Direct3D.Device Private m_sprite As Direct3D.Sprite Private m_texture As Direct3D.Texture Private m_devicelost As Boolean Private m_presentparams As PresentParameters Public Sub Dispose() m_device = Nothing m_sprite = Nothing m_texture = Nothing m_devicelost = Nothing m_presentparams = Nothing m_device.Dispose() m_sprite.Dispose() m_texture.Dispose() End Sub Public Sub New(ByRef frmHandle As IntPtr, ByVal FullScreen As Boolean) m_presentparams = New PresentParameters Dim Current As Format = Direct3D.Manager.Adapters(0).CurrentDisplayMode.Format m_presentparams.SwapEffect = SwapEffect.Discard If Direct3D.Manager.CheckDeviceType(0, DeviceType.Hardware, Current, Current, False) And FullScreen Then m_presentparams.Windowed = False m_presentparams.BackBufferFormat = Current m_presentparams.BackBufferCount = 1 m_presentparams.BackBufferWidth = 1024 m_presentparams.BackBufferHeight = 768 Else m_presentparams.Windowed = True End If m_device = New Direct3D.Device(0, Direct3D.DeviceType.Hardware, frmHandle, CreateFlags.SoftwareVertexProcessing, m_presentparams) m_sprite = New Direct3D.Sprite(m_device) End Sub Public Sub Render() If m_devicelost Then AttemptRecovery() End If If m_device Is Nothing Then Return End If Try m_device.Clear(ClearFlags.Target, Color.Black, 1.0F, 0) m_device.BeginScene() m_sprite.Begin(SpriteFlags.AlphaBlend) m_sprite.Draw(m_texture, Rectangle.Empty, New Vector3(0, 0, 0), New Vector3(5.0F, 5.0F, 0.0F), Color.White) m_sprite.End() m_device.EndScene() m_device.Present() Catch ex As Exception m_devicelost = True End Try End Sub Public Sub LoadSprite(ByVal filename As String) m_texture = TextureLoader.FromFile(m_device, filename, 256, 256, D3DX.Default, 0, Format.Unknown, Pool.Default, Filter.Point, Filter.Point, Color.Magenta.ToArgb) End Sub Private Sub AttemptRecovery() ' when we lose the device, this code will run Debug.WriteLine("entered attemptrecovery sub") Try m_device.TestCooperativeLevel() 'Test for Exclusive control; if it's lacking, it'll throw Catch e As DeviceLostException 'DeviceLostException. If it does, we just wait until 'Failed. 'our game is given the focus again. Catch e As DeviceNotResetException 'If we have the focus, we only need to reset the device: ' Basically we re-do what we did in initialize graphics Try 'Here, you need to destroy everything in Pool_Default, and all instances of the ' swapchain class. m_device.Reset(m_presentparams) 'reset the device, so we can use it again m_devicelost = False 'device is no longer lost!!! LoadSprite("Sprite.bmp") 'Now recreate all your Pool_Default stuff, and your swapchain instances. Debug.WriteLine("device recaptured") Catch bug As DeviceLostException 'it is possible to lose the device while resetting it. 'if it got lost again or not reset, do nothing Debug.WriteLine("device recapture failed") End Try End Try End Sub 'AttemptRecovery End Class