Professional Documents
Culture Documents
1
Adicionalmente utilicé otra función del API para reproducir archivos WAV,
por ejemplo: Al pulsar un botón del joystick.
sndPlaySound
Código fuente
frmJoystick
Option Explicit
'*******************************************************************
'Title : How work with some Joystick API functions
' (For Learning Uses)
'Author : DrakerDG
'Date : 09-05-2005
'e-mail : drakerdg@yahoo.com
'*******************************************************************
End Sub
2
Private Sub tbrJoy_ButtonClick(ByVal Button As MSComctlLib.Button)
'Toolbar click event
Select Case Button.Index
Case 3
mnuExit_Click
Case 5
mnuCJoy_Click
End Select
End Sub
Private Sub tmrScan_Timer()
'The scan joystick sub
Dim IDJoy As Long 'ID Joystick Value
With InfoJoyEX
DisButtX cmbJoy.ListIndex
IDJoy = joyGetPosEx(cmbJoy.ListIndex, InfoJoyEX) 'Get joystick
info.
stbInf.Panels(1).Text = " Joystick Status: " & JoyEst(IDJoy)
'Update the status bar
If .dwPOV <> Val(lblInfoEX(10).Caption) Then
bPOV = True
End If
'*** Update labels with InfoJoyEX ***
lblInfoEX(13).Caption = Str(IDJoy)
lblInfoEX(0).Caption = .dwSize
lblInfoEX(1).Caption = .dwFlags
lblInfoEX(2).Caption = .dwXpos
lblInfoEX(3).Caption = .dwYpos
lblInfoEX(4).Caption = .dwZpos
lblInfoEX(5).Caption = .dwRpos
lblInfoEX(6).Caption = .dwUpos
lblInfoEX(7).Caption = .dwVpos
lblInfoEX(8).Caption = .dwButtons
lblInfoEX(9).Caption = .dwButtonNumber
lblInfoEX(10).Caption = .dwPOV
lblInfoEX(11).Caption = .dwReserved1
lblInfoEX(12).Caption = .dwReserved2
End With
End Sub
'****************************************
'<<<< Personalized Sub-procedures. >>>>
'****************************************
Public Sub ButtonsX(ByVal xBs As Long)
'Init buttons scan procedure...
'For 1 to 32 buttons.
SetButtX xBs, 0 'Go to SetButX Sub...
SetButtX xBs, 1
SetButtX xBs, 2
SetButtX xBs, 3
SetButtX xBs, 4
SetButtX xBs, 5
3
SetButtX xBs, 6
SetButtX xBs, 7
SetButtX xBs, 8
SetButtX xBs, 9
SetButtX xBs, 10
SetButtX xBs, 11
SetButtX xBs, 12
SetButtX xBs, 13
SetButtX xBs, 14
SetButtX xBs, 15
SetButtX xBs, 16
SetButtX xBs, 17
SetButtX xBs, 18
SetButtX xBs, 19
SetButtX xBs, 20
SetButtX xBs, 21
SetButtX xBs, 22
SetButtX xBs, 23
SetButtX xBs, 24
SetButtX xBs, 25
SetButtX xBs, 26
SetButtX xBs, 27
SetButtX xBs, 28
SetButtX xBs, 29
SetButtX xBs, 30
SetButtX xBs, 31
End Sub
Public Sub DisButtX(ByVal xNbu As Integer)
'Set Enebled o Disabled the joystick buttons
Dim xRj As Long
Dim xCo As Integer
If (xNbu >= 0) And (xNbu <> Val(cmbJoy.Tag)) Then
xRj = joyGetDevCaps(cmbJoy.ListIndex, CapX, Len(CapX)) 'With
joyGetDevCaps function
For xCo = 0 To 31
If xCo < CapX.wNumButtons Then
Butt(xCo).BackColor = G01
Butt(xCo).BorderColor = BLK
NoX(xCo).ForeColor = BLK
Else
Butt(xCo).BackColor = G02
Butt(xCo).BorderColor = G01
NoX(xCo).ForeColor = G01
End If
Next xCo
cmbJoy.Tag = Trim(Str(xNbu))
End If
End Sub
Public Sub DrawAr(ByVal dwRes As Long)
'Move de arrow of the Z Axis and play sound (scroll left or scroll
right)
Dim Med, R1, R2, Ang As Integer
If dwRes <> xZbk Then
Med = Int(Joy(3).ScaleWidth / 2)
R1 = Joy(3).ScaleWidth - 25
R2 = Joy(3).ScaleWidth - 33
Joy(3).Cls
DrawOd
Joy(3).Scale (-1 * Med, R1 + 15)-(Med, -15)
Ang = Int(50 + dwRes * 80 / 65535)
Joy(3).DrawWidth = 2
4
Joy(3).Line (0, 0)- _
(R1 * Cos(Ang * PI / 180), R1 * Sin(Ang * PI / 180)),
R01
Ang = Ang + 180
Joy(3).Line (0, 0)- _
(10 * Cos(Ang * PI / 180), 10 * Sin(Ang * PI / 180)),
R01
Joy(3).Circle (0, 0), 5, BLK
If ((dwRes - SenX) > xZbk) Then
If InSn Then
InSn = False
Sound MPathX(App.Path) & "snd\Dn.wav", _
SND_ASYNC + SND_NODEFAULT
End If
End If
If ((dwRes + SenX) < xZbk) Then
If InSn Then
InSn = False
Sound MPathX(App.Path) & "snd\Up.wav", _
SND_ASYNC + SND_NODEFAULT
End If
End If
Joy(3).DrawWidth = 1
Else
InSn = True
End If
xZbk = dwRes
End Sub
Public Sub DrawCd(ByVal dwPO As Long)
'Select what direction in POV is pressed
Dim xCa As Integer
If dwPO <> xPOV Then
For xCa = 0 To 7
If Arw(xCa).Visible Then
Arw(xCa).Visible = False
End If
Next xCa
End Sub
Public Sub DrawCz(ByVal xInd As Integer, ByVal Xval As Long, ByVal
Yval As Long)
5
'Show de "+" pointer position for the X+Y Axis an U+R Axis.
Dim Xwi, Yhe As Long
Xwi = Int(Xval * (Joy(xInd).ScaleWidth - 1) / 65535)
Yhe = Int(Yval * (Joy(xInd).ScaleHeight - 1) / 65535)
Joy(xInd).Cls
6
Arw(0).Move R1 * Cos(90 * PI / 180) - 1 * Arw(0).Width / 2, R1 *
Sin(90 * PI / 180)
Arw(1).Move R1 * Cos(45 * PI / 180) - 1 * Arw(0).Width, R1 * Sin(45
* PI / 180)
Arw(2).Move R1 * Cos(0) - 1 * Arw(0).Width, R1 * Sin(0) + 1 *
Arw(0).Width / 2
Arw(3).Move R1 * Cos(315 * PI / 180) - 1 * Arw(0).Width, R1 *
Sin(315 * PI / 180) + Arw(0).Width
Arw(4).Move R1 * Cos(270 * PI / 180) - 1 * Arw(0).Width / 2, R1 *
Sin(270 * PI / 180) + Arw(0).Width
Arw(5).Move R1 * Cos(225 * PI / 180), R1 * Sin(225 * PI / 180) +
Arw(0).Width
Arw(6).Move R1 * Cos(180 * PI / 180), Arw(0).Width / 2
Arw(7).Move R1 * Cos(135 * PI / 180), R1 * Sin(135 * PI / 180)
Joy(0).AutoRedraw = False
End Sub
Public Sub DrawOd()
'Draw the Z-Graph marks.
Dim Med, R1, R2, Ang As Integer
Med = Int(Joy(3).ScaleWidth / 2)
R1 = Joy(3).ScaleWidth - 25
R2 = Joy(3).ScaleWidth - 33
Joy(3).Scale (-1 * Med, R1 + 15)-(Med, -15)
For Ang = 50 To 130 Step 10
Joy(3).Line (R1 * Cos(Ang * PI / 180), R1 * Sin(Ang * PI /
180))- _
(R2 * Cos(Ang * PI / 180), R2 * Sin(Ang * PI /
180)), _
IIf(Ang < 80, R01, BLK)
Next Ang
'Joy(3).AutoRedraw = False
End Sub
Public Sub DrawPOV(ByVal xInd As Integer)
'Show the arrow in the POV
If Not (Arw(xInd).Visible) Then
Arw(xInd).Visible = True
End If
If bPOV Then
bPOV = False
Sound MPathX(App.Path) & "snd\XY.wav", _
SND_ASYNC + SND_NODEFAULT
End If
End Sub
Public Sub InitJoy()
'Get the joystick number in the system and about information.
Dim xJa As Long
Dim xRj As Long
Dim IDJk As Long
Dim xJn, Lin, Lsu As Integer
Dim sDJ As String
SIDJ(1) = -1
SIDJ(2) = -1
InfoJoyEX.dwFlags = JOY_RETURNALL 'Dec 128: Request for status
buttons.
InfoJoyEX.dwSize = &H40 'Dec 64
xJa = joyGetNumDevs() - 1
ReDim AIDJ(0 To xJa, 0 To 1) As Integer
7
x = "No of joysticks: " & Str(xJa) & ". " & Str(UBound(AIDJ)) &
Chr(13)
For xJn = 0 To UBound(AIDJ)
xRj = joyGetDevCaps(xJn, CapX, Len(CapX))
x.Caption = x.Caption & Str(xRj) & ", "
sDJ = "Joistick " & Trim(Str(xJn + 1))
Select Case xRj
Case JOYERR_NOERROR
sDJ = sDJ & Str(CapX.wNumAxes) & " Axes"
sDJ = sDJ & Str(CapX.wNumButtons) & " Buttons"
IDJk = joyGetPosEx(xJn, InfoJoyEX)
sDJ = sDJ & " " & JoyEst(IDJk)
AIDJ(xJn, 0) = IDJk
Case Else
AIDJ(xJn, 0) = xRj
sDJ = sDJ & " (Not Driver Instaled)"
End Select
'AIDJ(xJn, 1) = CapX.wNumAxes
'AIDJ(xJn, 2) = CapX.wNumButtons
'AIDJ(xJn, 3) = CapX.wPid
cmbJoy.AddItem sDJ
Next xJn
cmbJoy.ListIndex = 0
End Sub
8
Buttons number
IDJk = joyGetPosEx(iCn, InfoJoyEX) 'Get
Joystick status
sDJ = sDJ & " " & JoyEst(IDJk) 'Write
Joystick status
AIDJ(iAr, 0) = IDJk
cmbJoy.AddItem sDJ
Case Else
AIDJ(xJn, 0) = -1
End Select
Next iCn
End Sub
modJoystick
Option Explicit
'*** Color constants ***
Public Const R01 = &HFF 'Red
Public Const G01 = &H808080 'Gray
Public Const G02 = &HC0C0C0 'Ligth Gray
Public Const BLK = &H0 'Black
'*** Math constants ***
Public Const PI = 3.14159265358979
'*** File constants ***
Public Const FSIZE = 32
'*** joyGetPosEx Constants ***
Public Const JOY_POVCENTERED = &HFFFF
Public Const JOY_POVFORWARD = &H0
Public Const JOY_POVFRDRHT = &H1194
Public Const JOY_POVRIGHT = &H2328
Public Const JOY_POVBRDRHT = &H34BC
Public Const JOY_POVBACKWARD = &H4650
Public Const JOY_POVBRDLFT = &H57E4
Public Const JOY_POVLEFT = &H6978
Public Const JOY_POVFRDLFT = &H7B0C
Public Const JOY_RETURNALL = &HFF
'*** sndPlaySound constants ***
Public Const SND_ASYNC = &H1
Public Const SND_NODEFAULT = &H2
Public Const SND_SYNC = &H0
'*** Calibrate constants ***
Public Const SenX = 1023 ' Sensitivity range to POV.
Public Const XY4D = 16383 ' Sensitivity Down limit to
X+Y Axis.
Public Const XY4U = 49152 ' Sensitivity Up limit to X+Y
Axis.
'*** Joystick constants ***
Public Const MMSYSERR_BASE As Long = &H0
Public Const MMSYSERR_NODRIVER = (MMSYSERR_BASE + 6) ' No
device driver present
Public Const MMSYSERR_INVALPARAM = (MMSYSERR_BASE + 11) ' Invalid
parameter passed
Public Const JOYERR_NOERROR = &H0 ' No error
Public Const JOYERR_BASE = &HA0
Public Const JOYERR_PARMS = (JOYERR_BASE + 5) ' Bad ID
parameters
Public Const JOYERR_NOCANDO = (JOYERR_BASE + 6) ' Request not
9
completed
Public Const JOYERR_UNPLUGGED = (JOYERR_BASE + 7) ' Joystick is
unplugged
'*** Joystick registers ***
Type JOYINFOEX
dwSize As Long ' size of structure
dwFlags As Long ' flags to indicate what to
return
dwXpos As Long ' x position
dwYpos As Long ' y position
dwZpos As Long ' z position
dwRpos As Long ' rudder/4th axis position
dwUpos As Long ' 5th axis position
dwVpos As Long ' 6th axis position
dwButtons As Long ' button states
dwButtonNumber As Long ' current button number
pressed
dwPOV As Long ' point of view state
dwReserved1 As Long ' reserved for communication
between winmm driver
dwReserved2 As Long ' reserved for future
expansion
End Type
Type JOYCAPS
wMid As Integer ' Manufacturer identifier of the
device driver for the MIDI output device
wPid As Integer ' Product Identifier Product of the
MIDI output device.
szPname As String * 32
wXmin As Long
wXmax As Long
wYmin As Long
wYmax As Long
wZmin As Long
wZmax As Long
wNumButtons As Long
wPeriodMin As Long ' Smallest polling frequency
supported when captured by the joySetCapture function.
wPeriodMax As Long ' Largest polling frequency supported
when captured by the joySetCapture function.
wRmin As Long
wRmax As Long
wUmin As Long
wUmax As Long
wVmin As Long
wVmax As Long
wCaps As Long
wMaxAxes As Long
wNumAxes As Long
wMaxButtons As Long
szRegKey As String * 32
szOEMVxD As String * 260
End Type
'*** Joystick API functions ***
Declare Function joyGetPosEx Lib "WinMM.dll" (ByVal uJoyID As Long, _
ByRef pji As JOYINFOEX) As Long
Declare Function joyGetDevCaps Lib "WinMM.dll" Alias "joyGetDevCapsA"
_
(ByVal ID As Long, ByRef lpCaps As JOYCAPS, ByVal
uSize As Long) As Long
Declare Function joyGetNumDevs Lib "WinMM.dll" () As Long
Declare Function sndPlaySound Lib "WinMM.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long)
As Long
10
Declare Function joySetCapture Lib "WinMM.dll" ( _
ByVal hWnd As Long, ByVal uJoyID As Long, _
ByVal uPeriod As Long, ByVal fChanged As Long) As
Long
Declare Function joyReleaseCapture Lib "WinMM.dll" ( _
ByVal uJoyID As Long) As Long
11
xRes = "Unplugged"
Case Else
xRes = "Unknown"
End Select
JoyEst = "(" & xRes & ") " & IDx 'Cambio
End Function
Public Function MPathX(ByVal sPhX As String) As String
'Add inverted slash if this not exist
Dim sPhY As String
sPhY = sPhX
If Mid(sPhY, Len(sPhY), 1) <> "\" Then
sPhY = sPhY & "\"
End If
MPathX = sPhY
End Function
12