游戏操作杆数据的读取
如题,最近想读北通c318操作杆的数据内容,找了directx7中一个VB6.0的例子,但升级到vb2005之后数据读不了,请各位大侠帮看下是怎么回事,小弟先谢了vb2005的代码如下:
Imports DxVBLib
Friend Class frmMain
Inherits System.Windows.Forms.Form
Implements DxVBLib.DirectXEvent
Dim dx As New DirectX7
Dim di As DxVBLib.DirectInput
Dim diDev As DxVBLib.DirectInputDevice
Dim diDevEnum As DxVBLib.DirectInputEnumDevices
Dim EventHandle As Integer
Dim xx As System.IntPtr
Dim joyCaps As DxVBLib.DIDEVCAPS
'UPGRADE_WARNING: 结构 js 中的数组可能需要先初始化才可以使用。 单击以获得更多信息:“ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="814DF224-76BD-4BB4-BFFB-EA359CB9FC48"”
Dim js As DxVBLib.DIJOYSTATE
Dim DiProp_Dead As DxVBLib.DIPROPLONG
Dim DiProp_Range As DxVBLib.DIPROPRANGE
Dim DiProp_Saturation As DxVBLib.DIPROPLONG
'UPGRADE_WARNING: 数组 AxisPresent 的下限已由 1 更改为 0。 单击以获得更多信息:“ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"”
Dim AxisPresent(0 To 8) As Boolean
Dim running As Boolean
Sub InitDirectInput()
di = dx.DirectInputCreate()
diDevEnum = di.GetDIEnumDevices(DxVBLib.CONST_DIDEVICETYPE.DIDEVTYPE_JOYSTICK, DxVBLib.CONST_DIENUMDEVICESFLAGS.DIEDFL_ATTACHEDONLY)
If diDevEnum.GetCount = 0 Then
MsgBox("No joystick attached.")
Me.Close()
End If
'Add attached joysticks to the listbox
Dim i As Short
For i = 1 To diDevEnum.GetCount
Call lstJoySticks.Items.Add(diDevEnum.GetItem(i).GetInstanceName)
Next
Dim ss As String = diDevEnum.GetItem(1).GetUsage()
' Get an event handle to associate with the device
EventHandle = dx.CreateEvent(Me)
Exit Sub
Error_Out:
MsgBox("Error initializing DirectInput.")
Me.Close()
End Sub
Private Sub DirectXEvent_DXCallback(ByVal eventid As Integer) Implements DxVBLib.DirectXEvent.DXCallback
' This is called whenever there's a change in the joystick state.
' We check the new state and update the display.
Dim i As Short
Dim ListPos As Short
Dim S As String
If diDev Is Nothing Then Exit Sub
'' Get the device info
On Error Resume Next
diDev.GetDeviceStateJoystick(js)
If Err.Number = DxVBLib.CONST_DINPUTERR.DIERR_NOTACQUIRED Or Err.Number = DxVBLib.CONST_DINPUTERR.DIERR_INPUTLOST Then
diDev.Acquire()
Exit Sub
End If
On Error GoTo err_out
' Display axis coordinates
ListPos = 0
For i = 1 To 8
If AxisPresent(i) Then
Select Case i
Case 1
S = "X: " & js.x
Case 2
S = "Y: " & js.y
Case 3
S = "Z: " & js.z
Case 4
S = "RX: " & js.rx
Case 5
S = "RY: " & js.ry
Case 6
S = "RZ: " & js.rz
Case 7
S = "Slider0: " & js.slider(0)
Case 8
S = "Slider1: " & js.slider(1)
End Select
VB6.SetItemString(lstJoyAxis, ListPos, S)
ListPos = ListPos + 1
End If
Next
' Buttons
For i = 0 To joyCaps.lButtons - 1
Select Case js.buttons(i)
Case 0
VB6.SetItemString(lstButton, i, "Button " & CStr(i + 1) & ": Up")
Case Else
VB6.SetItemString(lstButton, i, "Button " & CStr(i + 1) & ": Down")
End Select
Next
' Hats
For i = 0 To joyCaps.lPOVs - 1
VB6.SetItemString(lstHat, i, "POV " & CStr(i + 1) & ": " & CStr(js.POV(i)))
Next
Me.Text = "Joystick Sample: Available"
Exit Sub
err_out:
MsgBox(Err.Description & " : " & Err.Number, MsgBoxStyle.ApplicationModal)
End
End Sub
Private Sub frmMain_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave
End Sub
Private Sub frmMain_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
running = True
InitDirectInput()
End Sub
Private Sub frmMain_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
If EventHandle <> 0 Then dx.DestroyEvent(EventHandle)
running = False
System.Windows.Forms.Application.DoEvents()
End
End Sub
'UPGRADE_WARNING: 初始化窗体时可能激发事件 lstJoySticks.SelectedIndexChanged。 单击以获得更多信息:“ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"”
Private Sub lstJoySticks_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles lstJoySticks.SelectedIndexChanged
On Error Resume Next
Call CLRLISTS()
'Create the joystick device
'UPGRADE_NOTE: 在对对象 diDev 进行垃圾回收前,不可以将其销毁。 单击以获得更多信息:“ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"”
'diDev = Nothing
diDev = di.CreateDevice(diDevEnum.GetItem(lstJoySticks.SelectedIndex + 1).GetGuidInstance)
diDev.SetCommonDataFormat(DxVBLib.CONST_DICOMMONDATAFORMATS.DIFORMAT_JOYSTICK)
diDev.SetCooperativeLevel(Me.Handle.ToInt32, DxVBLib.CONST_DISCLFLAGS.DISCL_BACKGROUND Or DxVBLib.CONST_DISCLFLAGS.DISCL_NONEXCLUSIVE)
' Find out what device objects it has
diDev.GetCapabilities(joyCaps)
Call IdentifyAxes(diDev)
' Ask for notification of events
Call diDev.SetEventNotification(EventHandle)
' Set deadzone for X and Y axis to 10 percent of the range of travel
With DiProp_Dead
.lData = 1000
.lObj = DxVBLib.CONST_DIJOYSTICKOFS.DIJOFS_X
.lSize = Len(DiProp_Dead)
.lHow = DxVBLib.CONST_DIPHFLAGS.DIPH_BYOFFSET
.lObj = DxVBLib.CONST_DIJOYSTICKOFS.DIJOFS_X
diDev.SetProperty("DIPROP_DEADZONE", DiProp_Dead.ToString)
.lObj = DxVBLib.CONST_DIJOYSTICKOFS.DIJOFS_Y
diDev.SetProperty("DIPROP_DEADZONE", DiProp_Dead.ToString)
End With
' Set saturation zones for X and Y axis to 5 percent of the range
With DiProp_Saturation
.lData = 9500
.lHow = DxVBLib.CONST_DIPHFLAGS.DIPH_BYOFFSET
.lSize = Len(DiProp_Saturation)
.lObj = DxVBLib.CONST_DIJOYSTICKOFS.DIJOFS_X
diDev.SetProperty("DIPROP_SATURATION", DiProp_Saturation.ToString)
.lObj = DxVBLib.CONST_DIJOYSTICKOFS.DIJOFS_Y
diDev.SetProperty("DIPROP_SATURATION", DiProp_Saturation.ToString)
End With
SetProp()
diDev.Acquire()
Me.Text = "Joystick Sample: Querying Properties"
' Get the list of current properties
' USB joysticks wont call this callback until you play with the joystick
' so we call the callback ourselves the first time
DirectXEvent_DXCallback(0)
' Poll the device so that events are sure to be signaled.
' Usually this would be done in Sub Main or in the game rendering loop.
While running = True
System.Windows.Forms.Application.DoEvents()
diDev.Poll()
End While
End Sub
Sub SetProp()
' Set range for all axes
With DiProp_Range
.lHow = DxVBLib.CONST_DIPHFLAGS.DIPH_DEVICE
.lSize = Len(DiProp_Range)
.lMin = 0
.lMax = 10000
End With
diDev.SetProperty("DIPROP_RANGE", DiProp_Range.ToString)
End Sub
Sub CLRLISTS()
lstJoyAxis.Items.Clear()
lstButton.Items.Clear()
lstHat.Items.Clear()
End Sub
Sub IdentifyAxes(ByRef diDev As DxVBLib.DirectInputDevice)
' It's not enough to count axes; we need to know which in particular
' are present.
Dim didoEnum As DxVBLib.DirectInputEnumDeviceObjects
Dim dido As DxVBLib.DirectInputDeviceObjectInstance
Dim i As Short
For i = 1 To 8
AxisPresent(i) = False
Next
' Enumerate the axes
didoEnum = diDev.GetDeviceObjectsEnum(DxVBLib.CONST_DIDFTFLAGS.DIDFT_AXIS)
' Check data offset of each axis to learn what it is
For i = 1 To didoEnum.GetCount
dido = didoEnum.GetItem(i)
Select Case dido.GetOfs
Case DxVBLib.CONST_DIJOYSTICKOFS.DIJOFS_X
AxisPresent(1) = True
Case DxVBLib.CONST_DIJOYSTICKOFS.DIJOFS_Y
AxisPresent(2) = True
Case DxVBLib.CONST_DIJOYSTICKOFS.DIJOFS_Z
AxisPresent(3) = True
Case DxVBLib.CONST_DIJOYSTICKOFS.DIJOFS_RX
AxisPresent(4) = True
Case DxVBLib.CONST_DIJOYSTICKOFS.DIJOFS_RY
AxisPresent(5) = True
Case DxVBLib.CONST_DIJOYSTICKOFS.DIJOFS_RZ
AxisPresent(6) = True
Case DxVBLib.CONST_DIJOYSTICKOFS.DIJOFS_SLIDER0
AxisPresent(7) = True
Case DxVBLib.CONST_DIJOYSTICKOFS.DIJOFS_SLIDER1
AxisPresent(8) = True
End Select
Next
End Sub
End Class
[ 本帖最后由 zc6618 于 2010-1-21 10:42 编辑 ]