Use game joystick to touch the mouse

Source: Internet
Author: User
Tags bool time interval touch

To write an application that supports the joystick, you first have to capture the joystick, then process the joystick message that Windows sends to the program window, and then, after you finish using the joystick, release the captured joystick resources.

Call API function Joysetcapture can capture game joystick. When the Joysetcapture function is invoked, all messages generated by the joystick will be sent to the specified window. Its prototype is:

Mmresult Joysetcapture (HWND hwnd, UINT Ujoyid, UINT uperiod, BOOL fchanged);

Where the parameter HWND is the window handle that receives the joystick message, the parameter Ujoyid is the joystick ID to be captured, it can be JOYSTICKID1 or JOYSTICKID2, that is, the first and second game joysticks; parameter uperiod the frequency of polling, in milliseconds, It specifies a time interval for the application to send information about the joystick, and the parameter fchanged to change the position identity, set to false.

To release the capture of the joystick, use the Joyreleasecapture function. It has only one parameter, that is, the joystick's logo JOYSTICKID1 or JOYSTICKID2.

Below, let us use Borland C + + Builder 5.0来 to do a game joystick simulation mouse program.

Run Borland C + + Builder 5.0, double-click the form Form1, and add the following code to the Form1 OnCreate event to capture a game joystick:

void __fastcall TForm1::FormCreate(Tobject *Sender)
{
int JoyMsg;
//捕获游戏操纵杆
JoyMsg=joySetCapture(Handle,JOYSTICKID1,0,false);
if(JoyMsg==JOYERR_NOCANDO)
{
//捕获失败
ShowMessage("不能捕获游戏杆!");
}
else
{
if(JoyMsg==JOYERR_UNPLUGGED)
{
//没有连接
ShowMessage("游戏杆未与系统连接!");
}
else
{
if(JoyMsg==MMSYSERR_NODRIVER)
{
//没有安装
ShowMessage("系统没有安装游戏杆!");
}
else
{
//捕获成功
ShowMessage("捕获游戏杆成功!");
}
}
}
在Form1的OnCloseQuery事件中加入代码,让程序关闭时释放操纵杆捕获的资源:
void __fastcall TForm1::FormCloseQuery(Tobject *Sender, bool &CanClose)
{
//释放操纵杆捕获
joyReleaseCapture(JOYSTICKID1);
}

After capturing the joystick, Windows sends all joystick messages to the window Form1. When the joystick's direction button presses is pressed, produces is the Mm_joy1move message, when the function button is pressed, produces the mm_joy1buttondown message. You can simulate the movement and click of the mouse by responding to and handling the two messages separately in your program.

But in C + + Builder, these two messages are not standard Windows messages, which requires that we customize and process the messages. The steps to respond to a custom message in C + + Builder are:

1. Create a Message mapping table

2. Declaring a message handler function

3. Write Message handler function

First in the Code editing window right-click, select the pop-up menu "open Source/header File" or press the Hot key Ctrl+f6, open the form Form1 header file "Uint1.h."

Add code to the public member in the form's TForm1 class to establish the message map, handing the message processing to the custom message handler function:

Public

Begin_message_map

Message_handler (Mm_joy1buttondown,tmessage,onjoydown)

Message_handler (Mm_joy1move,tmessage,onjoymove)

End_message_map (Tform)

Then add the code declaration message handler function to the private members of the class:

Private

void __fastcall Onjoydown (tmessage &message);

void __fastcall Onjoymove (tmessage &message);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.