DirectX:DirectInput:Tutorials:VBNET:DX9:Mouse Handling

From GPWiki
Jump to: navigation, search

Minimal code you can use to demonstrate how to get mouse input using managed DX9 ...

Download this source.

Imports Microsoft.DirectX Imports System.Windows.Forms Imports Microsoft.DirectX.DirectInput.CooperativeLevelFlags

Public Class Form1

   Inherits System.Windows.Forms.Form
   Dim Mouse As New DirectInput.Device(DirectInput.SystemGuid.Mouse)
   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Mouse.SetCooperativeLevel(Me, Background Or NonExclusive)
       Me.Show()
       Mouse.Acquire()
     End Sub
   Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
       Dim Msg As String = ""
       Dim MS As DirectInput.MouseState = Mouse.CurrentMouseState
       Msg = ""
       Msg = "X: " & ms.X.ToString & vbCrLf
       Msg &= "Y: " & ms.Y.ToString & vbCrLf
       Msg &= "Z: " & ms.Z.ToString & vbCrLf
       Msg &= "Button 0: " & MS.GetMouseButtons(0).ToString & vbCrLf
       Msg &= "Button 1: " & MS.GetMouseButtons(1).ToString & vbCrLf
       Msg &= "Button 2: " & MS.GetMouseButtons(2).ToString & vbCrLf
       Label1.Text = Msg
   End Sub
   Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
       Mouse.Dispose()
       Mouse = Nothing
   End Sub

End Class