_NOERROR )
{
Connected = true;
JoystickID = JOYSTICKID1;
}
//如果发生INVALIDPARAM错误,则退出。
else if(JoyResult == MMSYSERR_INVALPARAM)
Application->MessageBox("An error occured while calling joyGetPos",
"Error", MB_OK);
// 如果第一个操纵杆为连接,则检查第二个操纵杆。
else if((JoyResult=joyGetPos(JOYSTICKID2,&JoyInfo)) == JOYERR_NOERROR)
{
Connected = true;
JoystickID = JOYSTICKID2;
}
}
}
在确定操纵杆已正确连接之后就可以读取操纵杆的设备信息。
void TForm1::ShowDeviceInfo(void)
{
joyGetDevCaps(JoystickID,&JoyCaps, sizeof(JOYCAPS));
Label1->Caption = "Number of joysticks supported by driver = " +
IntToStr(DriverCount);
Label2->Caption = "Current Joystick ID = " +
IntToStr(intJoystickID);
Label3->Caption = "Manufacturer ID = " +
IntToStr(JoyCaps.wMid);
Label4->Caption = "Product ID = " +
IntToStr(JoyCaps.wPid);
Label5->Caption = "Number of buttons = "+
IntToStr(JoyCaps.wNumButtons);
.
.
.
// 设置当前窗口接收操纵杆信息。
if(Connected)
joySetCapture(Handle,JoystickID,2*JoyCaps.wPeriodMin,FALSE);
//计算操纵杆活动范围和屏幕范围的比率,在后面绘制操纵杆标志时会用到。
XDivider = (JoyCaps.wXmax - JoyCaps.wXmin)/ Width;
YDivider = (JoyCaps.wYmax - JoyCaps.wYmin)/ Height;
}
读取操纵杆位置信息和按钮状态:
void TForm1::ShowStatusInfo(void)
{
if(Connected)
{
JOYINFO JoyInfo;
TPoint Position;
joyGetPos(JoystickID,&JoyInfo);
Position.x = JoyInfo.wXpos;
Position.y = JoyInfo.wYpos;
//显示操纵杆的X、Y轴位置。
Label6->Caption = "X Position = " + IntToStr(int(JoyInfo.wXpos));
Label7->Caption = "Y Position = " + IntToStr(int(JoyInfo.wYpos));
//判断某按钮是否被按下,这里只是指按钮初始的状态。
if(JoyInfo.wButtons & JOY_BUTTON1)
Label8->Caption = "Button 1 = Pressed";
else
Label8->Caption = "Button 1 = Not Pressed";
}
}
下面可以编写用以响应当初在头文件中定义的事件JMMove、JMButtonUpdate的代码: JMButtonUpdate的代码:
void __fastcall TForm1::JMMove(TMessage &msg)
{
/*当操纵杆位置发生变化时会自动调用本函数。
在本函数中经常是根据操纵杆当前的位置来绘制操纵杆在屏幕上显示的标志,并擦 去原来的标志。这里只是简单的改变Image的坐标位置来表示操纵杆为的移动。 */
Position.x = msg.LParamLo;
Position.y = msg.LParamHi;
//计算新的坐标。
ScreenX = (Position.x- |