home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / sources / chapter11 / MouseTest / Form1.frm (.txt) next >
Encoding:
Visual Basic Form  |  2004-10-22  |  4.4 KB  |  130 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "Form1"
  5.    ClientHeight    =   3300
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   4800
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    MinButton       =   0   'False
  12.    ScaleHeight     =   220
  13.    ScaleMode       =   3  'Pixel
  14.    ScaleWidth      =   320
  15.    StartUpPosition =   2  'CenterScreen
  16. Attribute VB_Name = "Form1"
  17. Attribute VB_GlobalNameSpace = False
  18. Attribute VB_Creatable = False
  19. Attribute VB_PredeclaredId = True
  20. Attribute VB_Exposed = False
  21. '----------------------------------------------------------------------
  22. ' Visual Basic Game Programming For Teens
  23. ' MouseTest Program
  24. '----------------------------------------------------------------------
  25. Option Explicit
  26. Option Base 0
  27. Implements DirectXEvent8
  28. 'DirectX objects and structures
  29. Public dx As New DirectX8
  30. Public di As DirectInput8
  31. Public diDev As DirectInputDevice8
  32. Dim didevstate As DIMOUSESTATE
  33. 'program constants and variables
  34. Const BufferSize = 10
  35. Public EventHandle As Long
  36. Public Drawing As Boolean
  37. Public Suspended As Boolean
  38. Private Sub Form_Load()
  39.     Me.Caption = "MouseTest"
  40.     Me.Show
  41.     'create the DirectInput object
  42.     Set di = dx.DirectInputCreate
  43.     'create the mouse object
  44.     Set diDev = di.CreateDevice("guid_SysMouse")
  45.     'configure DirectInputDevice to support the mouse
  46.     Call diDev.SetCommonDataFormat(DIFORMAT_MOUSE)
  47.     Call diDev.SetCooperativeLevel(Form1.hWnd, DISCL_FOREGROUND Or _
  48.         DISCL_EXCLUSIVE)
  49.     'set properties for the mouse device
  50.     Dim diProp As DIPROPLONG
  51.     diProp.lHow = DIPH_DEVICE
  52.     diProp.lObj = 0
  53.     diProp.lData = BufferSize
  54.     Call diDev.SetProperty("DIPROP_BUFFERSIZE", diProp)
  55.     'create mouse callback event handler
  56.     EventHandle = dx.CreateEvent(Form1)
  57.     diDev.SetEventNotification EventHandle
  58.     'acquire the mouse
  59.     diDev.Acquire
  60. End Sub
  61. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  62.     If KeyCode = 27 Then Shutdown
  63. End Sub
  64. Private Sub Shutdown()
  65.     On Local Error Resume Next
  66.     dx.DestroyEvent EventHandle
  67.     Set diDev = Nothing
  68.     Set di = Nothing
  69.     Set dx = Nothing
  70.     End
  71. End Sub
  72. Private Sub DirectXEvent8_DXCallback(ByVal eventid As Long)
  73.     Dim diDeviceData(1 To BufferSize) As DIDEVICEOBJECTDATA
  74.     Static lMouseX As Long
  75.     Static lMouseY As Long
  76.     Static lOldSeq As Long
  77.     Dim n As Long
  78.     'loop through events
  79.     For n = 1 To diDev.GetDeviceData(diDeviceData, 0)
  80.         Select Case diDeviceData(n).lOfs
  81.             Case DIMOFS_X
  82.                 lMouseX = lMouseX + diDeviceData(n).lData
  83.                 If lMouseX < 0 Then lMouseX = 0
  84.                 If lMouseX >= Form1.ScaleWidth Then
  85.                     lMouseX = Form1.ScaleWidth - 1
  86.                 End If
  87.                 If lOldSeq <> diDeviceData(n).lSequence Then
  88.                     Debug.Print "MouseMove: " & lMouseX & " x " & lMouseY
  89.                     lOldSeq = diDeviceData(n).lSequence
  90.                 Else
  91.                     lOldSeq = 0
  92.                 End If
  93.             Case DIMOFS_Y
  94.                 lMouseY = lMouseY + diDeviceData(n).lData
  95.                 If lMouseY < 0 Then lMouseY = 0
  96.                 If lMouseY >= Form1.ScaleHeight Then
  97.                     lMouseY = Form1.ScaleHeight - 1
  98.                 End If
  99.                 
  100.                 If lOldSeq <> diDeviceData(n).lSequence Then
  101.                     Debug.Print "Mouse: " & lMouseX & " x " & lMouseY
  102.                     lOldSeq = diDeviceData(n).lSequence
  103.                 Else
  104.                     lOldSeq = 0
  105.                 End If
  106.             Case DIMOFS_BUTTON0
  107.                 If diDeviceData(n).lData > 0 Then
  108.                     Debug.Print "ButtonDown 1"
  109.                 Else
  110.                     Debug.Print "ButtonUp 1"
  111.                 End If
  112.                 
  113.             Case DIMOFS_BUTTON1
  114.                 If diDeviceData(n).lData > 0 Then
  115.                     Debug.Print "ButtonDown 2"
  116.                 Else
  117.                     Debug.Print "ButtonUp 2"
  118.                 End If
  119.         
  120.             Case DIMOFS_BUTTON2
  121.                 If diDeviceData(n).lData > 0 Then
  122.                     Debug.Print "ButtonDown 3"
  123.                 Else
  124.                     Debug.Print "ButtonUp 3"
  125.                 End If
  126.        
  127.         End Select
  128.     Next n
  129. End Sub
  130.