www.pudn.com > 怎样对游戏手柄编程的例子joystick.zip > Form1.frm


VERSION 5.00 
Begin VB.Form Form1  
   BackColor       =   &H00000000& 
   BorderStyle     =   0  'None 
   Caption         =   "Form1" 
   ClientHeight    =   5940 
   ClientLeft      =   1890 
   ClientTop       =   1500 
   ClientWidth     =   6690 
   ForeColor       =   &H00000000& 
   LinkTopic       =   "Form1" 
   LockControls    =   -1  'True 
   PaletteMode     =   1  'UseZOrder 
   ScaleHeight     =   5940 
   ScaleWidth      =   6690 
   ShowInTaskbar   =   0   'False 
   WindowState     =   2  'Maximized 
   Begin VB.Timer Timer2  
      Interval        =   1 
      Left            =   630 
      Top             =   1125 
   End 
   Begin VB.CommandButton cmd1  
      Caption         =   "Command1" 
      Height          =   495 
      Left            =   5085 
      TabIndex        =   2 
      Top             =   3780 
      Width           =   1215 
   End 
   Begin VB.TextBox Text2  
      Enabled         =   0   'False 
      Height          =   375 
      Left            =   1050 
      TabIndex        =   1 
      Top             =   300 
      Width           =   1035 
   End 
   Begin VB.TextBox Text1  
      Enabled         =   0   'False 
      Height          =   375 
      Left            =   60 
      TabIndex        =   0 
      Top             =   300 
      Width           =   1005 
   End 
   Begin VB.Timer Timer1  
      Interval        =   1 
      Left            =   225 
      Top             =   1125 
   End 
   Begin VB.Label Label4  
      BackColor       =   &H00000000& 
      BackStyle       =   0  'Transparent 
      ForeColor       =   &H0000FF00& 
      Height          =   255 
      Left            =   1230 
      TabIndex        =   6 
      Top             =   795 
      Width           =   450 
   End 
   Begin VB.Label Label3  
      BackColor       =   &H00000000& 
      BackStyle       =   0  'Transparent 
      Caption         =   "Joystick Button:" 
      ForeColor       =   &H0000FF00& 
      Height          =   255 
      Left            =   60 
      TabIndex        =   5 
      Top             =   795 
      Width           =   1200 
   End 
   Begin VB.Label Label2  
      BackColor       =   &H00000000& 
      BackStyle       =   0  'Transparent 
      Caption         =   "Y Coord:" 
      ForeColor       =   &H0000FF00& 
      Height          =   255 
      Left            =   1155 
      TabIndex        =   4 
      Top             =   30 
      Width           =   645 
   End 
   Begin VB.Label Label1  
      BackColor       =   &H00000000& 
      BackStyle       =   0  'Transparent 
      Caption         =   "X Coord:" 
      ForeColor       =   &H0000FF00& 
      Height          =   270 
      Left            =   135 
      TabIndex        =   3 
      Top             =   15 
      Width           =   690 
   End 
   Begin VB.Line Line2  
      BorderColor     =   &H0000FF00& 
      X1              =   -50 
      X2              =   12015 
      Y1              =   4020 
      Y2              =   4020 
   End 
   Begin VB.Line Line1  
      BorderColor     =   &H0000FF00& 
      X1              =   5700 
      X2              =   5700 
      Y1              =   -225 
      Y2              =   8775 
   End 
End 
Attribute VB_Name = "Form1" 
Attribute VB_GlobalNameSpace = False 
Attribute VB_Creatable = False 
Attribute VB_PredeclaredId = True 
Attribute VB_Exposed = False 
'怎样对游戏手柄编程呢?本例会给你答案 
 
 
Dim myJoy As JOYINFOEX 
 
Private Sub cmd1_KeyPress(KeyAscii As Integer) 
 
If KeyAscii = vbKeyEscape Then 
     
    ' Show the mouse cursor. 
     ShowCursor (True) 
   
  End 
   
End If 
 
End Sub 
 
Private Sub Form_GotFocus() 
 
' Set focus to the command button so the user can exit by pressing 
' the Escape key.  (the End statement is in cmd1_KeyPress() ) 
cmd1.SetFocus 
 
End Sub 
 
Private Sub Form_KeyPress(KeyAscii As Integer) 
 
' If user pressed Escape, end the program. 
If KeyAscii = vbKeyEscape Then 
  End 
End If 
 
End Sub 
 
Private Sub Form_Load() 
Dim r& 
Dim hWnd& 
Static TheX As Long 
Static TheY As Long 
 
' Hide the mouse cursor. 
ShowCursor (False) 
 
' Tell form to receive joystick functions. 
r& = joySetCapture(hWnd, JOYSTICKID1, 1, 0) 
r& = joyReleaseCapture(JOYSTICKID1) 
 
' Get joystick position coordinates and fill in the TheX and TheY 
' variables. 
r& = joyGetPosEx(JOYSTICKID1, myJoy) 
TheX = myJoy.dwXpos 
TheY = myJoy.dwYpos 
 
End Sub 
 
 
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) 
 
' Show the mouse cursor. 
ShowCursor (True) 
 
End Sub 
 
Private Sub Form_Resize() 
 
' Disable the timer. 
If Form1.WindowState = 0 Then Timer1.Enabled = False 
 
End Sub 
 
Private Sub Form_Terminate() 
 
' Show the mouse cursor. 
ShowCursor (True) 
 
End Sub 
 
Private Sub Form_Unload(Cancel As Integer) 
 
' Show the mouse cursor. 
ShowCursor (True) 
 
End Sub 
 
 
Private Sub Timer1_Timer() 
 
myJoy.dwSize = 64 
myJoy.dwFlags = JOY_RETURNALL 
 
' Get the joystick coordinates. 
r& = joyGetPosEx(JOYSTICKID1, myJoy) 
 
' If a joystick's button is pressed, put the number of the button in 
' label4. 
If myJoy.dwButtons > 0 Then 
  Label4.Caption = myJoy.dwButtons 
Else 
  Label4.Caption = "" 
End If 
 
' Check joystick coordinates.  If they are greater or less than the 
' center coordinates, move the lines and command button in the 
' appropriate direction and speed. 
If myJoy.dwXpos > 25450 And cmd1.Left < 10800 Then 
TheFirst = myJoy.dwXpos - 25450 
   Line1.X1 = Line1.X1 + (TheFirst / 10) 
   Line1.X2 = Line1.X2 + (TheFirst / 10) 
 cmd1.Left = cmd1.Left + (TheFirst / 10) 
End If 
If myJoy.dwYpos > 36000 And cmd1.Top < 7816 Then 
TheSecond = myJoy.dwYpos - 36000 
   Line2.Y1 = Line2.Y1 + (TheSecond / 10) 
   Line2.Y2 = Line2.Y2 + (TheSecond / 10) 
cmd1.Top = cmd1.Top + (TheSecond / 10) 
End If 
If myJoy.dwXpos < 25450 And cmd1.Left > 0 Then 
TheFirst = 25450 - myJoy.dwXpos 
   Line1.X1 = Line1.X1 - (TheFirst / 10) 
   Line1.X2 = Line1.X2 - (TheFirst / 10) 
cmd1.Left = cmd1.Left - (TheFirst / 10) 
End If 
If myJoy.dwYpos < 36000 And cmd1.Top > 0 Then 
TheSecond = 36000 - myJoy.dwYpos 
   Line2.Y1 = Line2.Y1 - (TheSecond / 10) 
   Line2.Y2 = Line2.Y2 - (TheSecond / 10) 
cmd1.Top = cmd1.Top - (TheSecond / 10) 
End If 
 
' Fill the textboxes with the joystick coordinates. 
Text1.Text = myJoy.dwXpos 
Text2.Text = myJoy.dwYpos 
 
End Sub 
 
 
Private Sub Timer2_Timer() 
 
' If the button is past the left border, the button is set ON the border. 
If cmd1.Left < 0 Then 
  cmd1.Left = 0 
End If 
 
' If the button is past the top border, the button is set ON the border. 
If cmd1.Top < 0 Then 
  cmd1.Top = 0 
End If 
 
' If the button is past the right border, the button is set ON the border. 
If cmd1.Left > 10800 Then 
  cmd1.Left = 10800 
End If 
 
' If the button is past the bottom border, the button is set ON the border. 
If cmd1.Top > 8312 Then 
  cmd1.Top = 8312 
End If 
 
' Set crosshair back under the command button. 
Line1.X1 = cmd1.Left + 600 
Line1.X2 = cmd1.Left + 600 
Line2.Y1 = cmd1.Top + 233 
Line2.Y2 = cmd1.Top + 233 
 
End Sub