How to Use DirectX in VB

104 6
    • 1). Right-click the project name in the navigation panel, and click "Add Reference." Add project references to Microsoft.DirectX.dll and Microsoft.DirectX.DirectDraw.dll.

    • 2). Copy and paste the following into the top of your Main.vb file:

      Imports Microsoft.DirectX
      Imports Microsoft.DirectX.DirectDraw

    • 3). At the top of your Controller object, paste the following variable declarations:

      Private display As Device
      Private front As Surface = Nothing
      Private back As Surface = Nothing
      Private title As Surface = Nothing
      Private text As Surface = Nothing
      Private clip As Clipper = Nothing
      Private titlescreen As String = Application.StartupPath + "\title.bmp"

    • 4). Paste the following code into your class below the constructor:

      Private Sub InitDirectDraw()
      ' Used to describe a Surface

      Dim description As New SurfaceDescription()
      ' Init the Device

      display = New Device()
      #If DEBUG Then
      display.SetCooperativeLevel(Me, CooperativeLevelFlags.Normal)
      #Else
      ' Set the Cooperative Level and parent,

      'Setted to Full Screen Exclusive to the form)

      display.SetCooperativeLevel(Me, CooperativeLevelFlags.FullscreenExclusive)
      ' Set the resolution and color depth

      'used in full screen(640x480, 16 bit color)

      display.SetDisplayMode(640, 480, 16, 0, False)
      #End If

      ' Define the attributes for the front Surface

      description.SurfaceCaps.PrimarySurface = True

      #If DEBUG Then
      front = New Surface(description, display)
      #Else
      description.SurfaceCaps.Flip = True
      description.SurfaceCaps.Complex = True

      ' Set the Back Buffer count

      description.BackBufferCount = 1

      ' Create the Surface with specifed description and device)

      front = New Surface(description, display)
      #End If
      description.Clear()
      #If DEBUG Then
      description.Width = front.SurfaceDescription.Width
      description.Height = front.SurfaceDescription.Height
      description.SurfaceCaps.OffScreenPlain = True
      back = New Surface(description, display)
      #Else
      ' A Caps is a set of attributes used by most of DirectX components

      Dim caps As New SurfaceCaps()
      ' Yes, we are using a back buffer

      caps.BackBuffer = True

      ' Associate the front buffer to back buffer with specified caps

      back = front.GetAttachedSurface(caps)
      #End If

      ' Create the Clipper

      clip = New Clipper(display)
      ''' Set the region to this form

      clip.Window = Me
      ' Set the clipper for the front Surface

      front.Clipper = clip

      ' Reset the description

      description.Clear()
      ' Create the title screen

      title = New Surface(titlescreen, description, display)

      description.Clear()
      ' Set the height and width of the text.

      description.Width = 600
      description.Height = 16
      ' OffScreenPlain means that this Surface

      'is not a front, back, alpha Surface.

      description.SurfaceCaps.OffScreenPlain = True

      ' Create the text Surface

      text = New Surface(description, display)
      ' Set the backgroup color

      text.ColorFill(Color.Black)
      ' Set the fore color of the text

      text.ForeColor = Color.White
      ' Draw the Text to the Surface to coords (0,0)

      text.DrawText(0, 0, "Managned DirectX Tutorial 1 - Press Enter or Escape to exit", True)
      End Sub
      Private Sub Draw()
      ' If the front isn't create, ignore this function

      If front Is Nothing Then
      Return
      End If

      ' If the form is minimized, ignore this function

      If Me.WindowState = FormWindowState.Minimized Then
      Return
      End If
      Try
      ' Draw the title to the back buffer using source copy blit

      back.DrawFast(0, 0, title, DrawFastFlags.Wait)

      ' Draw the text also to the back buffer using source copy blit

      back.DrawFast(10, 10, text, DrawFastFlags.Wait)

      #If DEBUG Then
      ' Draw all this to the front

      front.Draw(back, DrawFlags.Wait)
      #Else
      ' Doing a flip to transfer back buffer to the front, faster

      #End If
      front.Flip(back, FlipFlags.Wait)

      Catch generatedExceptionName As WasStillDrawingException
      Return
      Catch generatedExceptionName As SurfaceLostException
      ' If we lost the surfaces, restore the surfaces

      RestoreSurfaces()
      End Try
      End Sub

      Private Sub RestoreSurfaces()
      ' Used to describe a Surface

      Dim description As New SurfaceDescription()

      ' Restore al the surface associed with the device

      display.RestoreAllSurfaces()
      ' Redraw the text

      text.ColorFill(Color.Black)
      text.DrawText(0, 0, "Managned DirectX Tutorial 1 - Press Enter or Escape to exit", True)

      ' For the title screen, we need to

      'dispose it first and then re-create it

      title.Dispose()
      title = Nothing
      title = New Surface(titlescreen, description, display)
      Return
      End Sub

    • 5). Copy and paste the following code into your class constructor:

      InitializeComponent()
      InitDirectDraw()
      Me.Cursor.Dispose()
      Me.Show()
      While Created
      Draw()
      Application.DoEvents()
      End While

    • 6). Press "F5" to run your application and see the title.bmp being displayed on the screen.

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.