Private Sub Form_Load()
' Starta med timern avstängd
Timer1.Enabled = False
' Hämta egenskaper för joystick1
rc = joyGetDevCaps(JOYSTICKID1, caps, Len(caps))
If (rc <> 0) Then
MsgBox "Finner ingen joystick"
End
End If
' Addera kontroller för axeln
numAxes = 1
axisY = axis(0).Top
Set xaxis = axis(0)
AddAxisControl "Y", yaxis
If (caps.wCaps And JOYCAPS_HASZ) Then AddAxisControl "Z", zaxel
If (caps.wCaps And JOYCAPS_HASR) Then AddAxisControl "R", raxex
If (caps.wCaps And JOYCAPS_HASU) Then AddAxisControl "U", uaxel
If (caps.wCaps And JOYCAPS_HASV) Then AddAxisControl "V", vaxel
If (caps.wCaps And JOYCAPS_HASPOV) Then AddAxisControl "POV", pov
' Skapar checkboxas för knapparna
LoadButtonCheckBoxes (caps.wNumButtons)
' Positionerar status boxen
status.Left = 0
status.Top = Form1.Height - (status.Height * 2.5)
' Starta timern
Timer1.Enabled = True
End Sub
Private Sub AddAxisControl(name As String, ctrl As label)
'Addera label för att visa axlarna
axisY = axisY + (axis(0).Height * ySpacingFactor)
If (Form1.Height < (axisY + axis(0).Height * 3)) Then
Form1.Height = Form1.Height + (axis(0).Height * ySpacingFactor)
End If
Load axis(numAxes)
Load label(numAxes)
Set ctrl = axis(numAxes)
ctrl.Width = axis(0).Width
ctrl.Height = axis(0).Height
ctrl.Left = axis(0).Left
ctrl.Top = axisY
ctrl.Visible = True
label(numAxes).Width = label(0).Width
label(numAxes).Height = label(0).Height
label(numAxes).Left = label(0).Left
label(numAxes).Top = axisY
label(numAxes).Visible = True
label(numAxes).Caption = name
numAxes = numAxes + 1
End Sub
Private Sub LoadButtonCheckBoxes(numButtons As Long)
' Addera en checkbox för varje befintlig knapp på joysticken
If (numButtons = 0) Then
button(0).Visible = False
Form1.Width = button(0).Left
Else
If (numButtons > 1) Then
Dim curX As Long
Dim curY As Long
Dim i As Long
curX = button(0).Left
curY = button(0).Top
For i = 1 To (numButtons - 1)
' flytta ner för nästa kontroll
curY = curY + (button(0).Height * ySpacingFactor)
' starta en ny column om det behövs
If (Form1.Height < (curY + 3 * button(0).Height)) Then
curY = button(0).Top
curX = curX + button(0).Width
End If
' öka formen om det behövs
If (Form1.Width < (curX + button(0).Width)) Then
Form1.Width = curX + button(0).Width
End If
' sätt checkbox och dess egenskaper
Load button(i)
button(i).Top = curY
button(i).Left = curX
button(i).Width = button(0).Width
button(i).Height = button(0).Height
button(i).Visible = True
button(i).Caption = "button " & i + 1
Next
End If
End If
End Sub
Private Sub Timer1_Timer()
' Läs av joystick med intervall
' Initialisera structuren
ji.dwSize = Len(ji)
ji.dwFlags = JOY_RETURNALL
' Hämta aktuell joystick data
rc = joyGetPosEx(JOYSTICKID1, ji)
' Visa status
If (rc = 0) Then
status.Caption = "status: joystick ansluten"
Else
If (rc = JOYERR_UNPLUGGED) Then
status.Caption = "status: joystick saknas"
Else
status.Caption = "status: joyGetPosEx error, rc = " & rc
End If
End If
' Visa datainformationen i formen
xaxis.Caption = ji.dwXpos
yaxis.Caption = ji.dwYpos
If (caps.wCaps And JOYCAPS_HASZ) Then zaxis.Caption = ji.dwZpos
If (caps.wCaps And JOYCAPS_HASR) Then raxis.Caption = ji.dwRpos
If (caps.wCaps And JOYCAPS_HASU) Then uaxis.Caption = ji.dwUpos
If (caps.wCaps And JOYCAPS_HASV) Then vaxis.Caption = ji.dwVpos
If (caps.wCaps And JOYCAPS_HASPOV) Then pov.Caption = ji.dwPOV
mask = 1
For i = 0 To (caps.wNumButtons - 1)
If (ji.dwButtons And mask) Then button(i).Value = 1 Else button(i).Value = 0
mask = mask * 2
Next
End Sub