vb.net游戏手柄支持,部分工作

时间:2012-12-01 04:27:02

标签: vb.net class input gamepad-api

我在论坛上找到了以下类代码。它适用于游戏手柄(向上,向下,向左,向右)但是按钮的所有代码都缺失了。任何人都可以填补空白吗?

这有效:

Private Sub joystick1_Up() Handles joystick1.Up
    moveUp()
End Sub

这不是:

Private Sub joystick1_buttonPressed() Handles joystick1.buttonPressed
    MsgBox(joystick1.btnValue)
End Sub

因为没有“buttonPressed”事件,我不知道如何写它。

这是班级:

Imports System.ComponentModel
Imports System.Runtime.InteropServices


Public Class joystick
    Inherits NativeWindow

    Private parent As Form
    Private Const MM_JOY1MOVE As Integer = &H3A0

    ' Public Event Move(ByVal joystickPosition As Point)
    Public btnValue As String
    Public Event Up()
    Public Event Down()
    Public Event Left()
    Public Event Right()

    <StructLayout(LayoutKind.Explicit)> _
    Private Structure JoyPosition
        <FieldOffset(0)> _
        Public Raw As IntPtr
        <FieldOffset(0)> _
        Public XPos As UShort
        <FieldOffset(2)> _
        Public YPos As UShort
    End Structure

    Private Class NativeMethods

        Private Sub New()
        End Sub

        ' This is a "Stub" function - it has no code in its body.
        ' There is a similarly named function inside a dll that comes with windows called
        ' winmm.dll. 
        ' The .Net framework will route calls to this function, through to the dll file.
        <DllImport("winmm", CallingConvention:=CallingConvention.Winapi, EntryPoint:="joySetCapture", SetLastError:=True)> _
        Public Shared Function JoySetCapture(ByVal hwnd As IntPtr, ByVal uJoyID As Integer, ByVal uPeriod As Integer, <MarshalAs(UnmanagedType.Bool)> ByVal changed As Boolean) As Integer
        End Function

    End Class

    Public Sub New(ByVal parent As Form, ByVal joyId As Integer)
        AddHandler parent.HandleCreated, AddressOf Me.OnHandleCreated
        AddHandler parent.HandleDestroyed, AddressOf Me.OnHandleDestroyed
        AssignHandle(parent.Handle)
        Me.parent = parent
        Dim result As Integer = NativeMethods.JoySetCapture(Me.Handle, joyId, 100, True)
    End Sub

    Private Sub OnHandleCreated(ByVal sender As Object, ByVal e As EventArgs)
        AssignHandle(DirectCast(sender, Form).Handle)
    End Sub

    Private Sub OnHandleDestroyed(ByVal sender As Object, ByVal e As EventArgs)
        ReleaseHandle()
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = MM_JOY1MOVE Then
            ' Joystick co-ords.
            ' (0,0) (32768,0) (65535, 0) 
            '
            '
            '
            ' (0, 32768) (32768, 32768) (65535, 32768)
            '
            '
            '
            '
            ' (0, 65535) (32768, 65535) (65535, 65535)
            '

            Dim p As JoyPosition
            p.Raw = m.LParam
            ' RaiseEvent Move(New Point(p.XPos, p.YPos))
            If p.XPos > 16384 AndAlso p.XPos < 49152 Then
                ' X is near the centre line.
                If p.YPos < 6000 Then
                    ' Y is near the top.
                    RaiseEvent Up()
                ElseIf p.YPos > 59536 Then
                    ' Y is near the bottom.
                    RaiseEvent Down()
                End If
            Else
                If p.YPos > 16384 AndAlso p.YPos < 49152 Then
                    ' Y is near the centre line
                    If p.XPos < 6000 Then
                        ' X is near the left.
                        RaiseEvent Left()
                    ElseIf p.XPos > 59536 Then
                        ' X is near the right
                        RaiseEvent Right()
                    End If
                End If
            End If
        End If
        If btnValue <> m.WParam.ToString Then
            btnValue = m.WParam.ToString
        End If
        MyBase.WndProc(m)
    End Sub

End Class

1 个答案:

答案 0 :(得分:2)

不使用旧的winmm,而是使用XInput(或者如果可以的话,使用XNA)。

有几种方法可以做到这一点。一步是直接使用this question on the MSDN forums概述的XInput dll。但这仍然相当难看。执行此操作的“更简单”方法可能是使用存在于其中的包装库,如SlimDXSharpDX

通过SlimDX或SharpDX使用XInput的一个优点是它也适用于Windows 8的Windows应用商店应用程序:)。

以下是the GamePad sample in SharpDX的摘录:

C#

var controllers = new[] { new Controller(UserIndex.One), new Controller(UserIndex.Two), new Controller(UserIndex.Three), new Controller(UserIndex.Four) };

// Get 1st controller available 
Controller controller = null; 
foreach (var selectControler in controllers) 
{ 
    if (selectControler.IsConnected) 
    { 
        controller = selectControler; 
        break; 
    }
}

VB

Dim controllers As New List(Of Controller)
controllers.Add(New Controller(UserIndex.One))
controllers.Add(New Controller(UserIndex.Two))
controllers.Add(New Controller(UserIndex.Three))
controllers.Add(New Controller(UserIndex.Four))

Dim controller as Controller = Nothing; 
For Each selectController In controllers
    If selectController.IsConnected Then
        controller = selectController
        Exit For
    End If
Next

然后你可以使用以下方式获得使用状态:

var state = controller.GetState();

您会注意到XInput使用了更多的轮询模型,因此您需要偶尔检查按下按钮以检测此情况。如果你需要连续轮询,你可以启动一个新任务来执行此操作。