Programming the game pole with vc5.0

Source: Internet
Author: User
 

Programming the game pole with vc5.0

Liu litian

Bu gehong

There is a joystickproperties control panel in Windows95/98. You can enjoy the powerful functions provided by joystick as long as you have a sound card that supports the joystick connection and a game player.

How to Use

Visual c ++ is used to program the game rod, hoping to help you.

I

Capture and release players

How to Use

How does visual c ++ 5.0 program the game rod? First, you must be able to capture the game rod. Here we use the joysetcapture function to achieve this. The following is the definition of the joysetcapture function and the description of each parameter.

Int joysetcapture

(Hwnd, unit ujoyid, unit uperiod, bool fchanged );

[1] hwnd -----

Parent window handle.

[2] ujoyid ----

Specify the game lever, which can be joystickid1 or joystickid2.

[3] uperiod ----

Sends information about the game pole to the application at a given polling interval. This parameter

It is the round-robin frequency in subtle units.

The following is the program code for capturing a gamer's message.

Int cjaystickdlg: oncreate (maid)

{

Int result;

If (cdialog: oncreate (lpcreatestruct) =-1)

Return-1;

Result = joysetcapture (cdialog: m_hwnd, joystickid1, 0, false );

If (result = joyerr_nocando ){

Messagebeep (mb_iconexclamation );

MessageBox ("

Cannot capture the game pole ", null, mb_ OK | mb_iconexclamation );

Return-1;

}

If (result = joyerr_unplugged ){

Messagebeep (mb_iconexclamation );

MessageBox ("players are not connected to the system", null, mb_ OK | mb_iconexclamation );

Return-1;

}

Return 0;

}

Once you start capturing a gamer's message, you can check the messages returned from the device. In order to capture the game rod message, we wrote a windowproc process. The program list is as follows:

Lresult cjaystickdlg: windowproc (uint message, wparam, lparam)

{

Switch (Message ){

Case mm_joy1buttondown:

If (wparam & joy_button1 ){

Messagebeep (mb_iconquestion );

Setdlgitemtext (idc_button1, "button 1: <

Click> ");

}

If (wparam & joy_button2 ){

Messagebeep (mb_iconexclamation );

Setdlgitemtext (idc_button2, "button 2: <

Click> ");

}

Break;

Case mm_joy1buttonup:

If (wparam & joy_button1 ){

Setdlgitemtext (idc_button1, "button 1: <

Click> ");

Setdlgitemtext (idc_button2, "button 2 :");

}

Else if (wparam & joy_button2 ){

Setdlgitemtext (idc_button2, "button 2: <

Click> ");

Setdlgitemtext (idc_button1, "button 1 :");

}

Else {

Setdlgitemtext (idc_button1, "button 1 :");

Setdlgitemtext (idc_button2, "button 2 :");

}

Break;

Case mm_joy1move:

Word X, Y;

Point pt;

Updatedata (true );

//

Obtains the cursor position of the current screen.

Getcursorpos (& pt );

//

The coordinates of the game rod in the current system range from 0 to 65535. The position indicates that the range is from 0 to 31.

If (! M_absolute ){

X = loword (lparam)/2048;

Y = hiword (lparam)/2048;

//

If the player is on the left of the center, move the cursor to the left. And vice versa

If (x <= 12)

PT. x = pt. x + X-15;

Else if (x> = 20)

PT. x = pt. x + X-15;

//

Move the cursor to the bottom if the game rod is at the center. And vice versa

If (Y <= 12)

PT. Y = pt. Y + Y-15;

Else if (Y> = 20)

PT. Y = pt. Y + Y-15;

}

Else {

CDC * PDC = getdc ();

X = loword (lparam );

Y = hiword (lparam );

PT. x = (PDC-> getdevicecaps (horzres) * X)/65536;

PT. Y = (PDC-> getdevicecaps (vertres) * Y)/65536;

Releasedc (PDC );

}

/* Set the new cursor position .*/

Setcursorpos (Pt. X, Pt. y );

Break;

}

Return cdialog: windowproc (message, wparam, lparam );

}

In

Mm_joy1buttondown, mm_joy1buttonup, and mm_joy1move are used in windowproc to send messages to the game rod device. The parameter wparam of javasweproc stores the status information of the gamepad button. The parameter iparam stores Y coordinates at the 16-digit height (0 65 535) and X coordinates at the 16-digit height (0 65 535 ). Position (0, 0) indicates the upper left corner of the game rod position, and position (65, 535) indicates the lower right corner of the game rod position.

Ensure that the player is released after the player is used up. Use

The joyreleasecapture function. The following is the definition of the function:

Mmresult joyreleasecapture

(Unit ujoyid );

Where

Ujoyid is one of joystickid1 and joystickid2. The following program list shows how the game rod is released in the destroywindow event of the application.

Void cjaystickdlg: ondestroy ()

{

Cdialog: ondestroy ();

Joyreleasecapture (joystickid1 );

}

3.

Application Instance

A simple application is compiled based on the methods described in this article. This application creates a simple dialog window. After the application is started, whenever you press a button on the player bar, the button will appear next to the button text. When you move a game rod, the mouse cursor moves on the screen. When you release the game rod, the mouse cursor remains unchanged at the current position.

Figure

1

In

In the Visual C ++ 5.0 environment, use Appwizard to generate the dialog box program framework. Dialog Box 1 is displayed. You only need to edit joystdlg. cpp to implement this program. The following describes all the source code of joystdlg. cpp.

// Jaystdlg. cpp: implementation file

# Include "stdafx. H"

# Include "joystick. H"

# Include "joystdlg. H"

# Include "mmsystem. H"

# Ifdef _ debug

# UNDEF this_file

Static char based_code this_file [] = _ file __;

# Endif

//////////////////////////////////////// /////////////////////////////////////

// Caboutdlg Dialog used for app about

{

¼

}

//////////////////////////////////////// /////////////////////////////////////

// Cjaystickdlg Dialog

Cjaystickdlg: cjaystickdlg (cwnd * pparent/* = NULL */)

: Cdialog (cjaystickdlg: IDD, pparent)

{

// {Afx_data_init (cjaystickdlg)

M_absolute = false;

//} Afx_data_init

// Note that loadicon does not require a subsequent destroyicon in Win32

M_hicon = afxgetapp ()-> loadicon (idr_mainframe );

}

Void cjaystickdlg: dodataexchange (cdataexchange * PDX)

{

Cdialog: dodataexchange (PDX );

// {Afx_data_map (cjaystickdlg)

Ddx_check (PDX, idc_absolute, m_absolute );

//} Afx_data_map

}

Begin_message_map (cjaystickdlg, cdialog)

// {Afx_msg_map (cjaystickdlg)

On_wm_syscommand ()

On_wm_paint ()

On_wm_querydragicon ()

On_wm_create ()

On_wm_destroy ()

//} Afx_msg_map

End_message_map ()

//////////////////////////////////////// /////////////////////////////////////

// Cjaystickdlg message handlers

Bool cjaystickdlg: oninitdialog ()

{

Cdialog: oninitdialog ();

Centerwindow ();

// Todo: add extra initialization here

Return true; // return true unless you set the focus to a control

}

Void cjaystickdlg: onpaint ()

{

If (isiconic ())

{

Cpaintdc DC (this); // device context for painting

Sendmessage (wm_iconerasebkgnd, (wparam) DC. getsafehdc (), 0 );

// Center icon in client rectangle

Int cxicon = getsystemmetrics (sm_cxicon );

Int cyicon = getsystemmetrics (sm_cyicon );

Crect rect;

Getclientrect (& rect );

Int x = (rect. Width ()-cxicon + 1)/2;

Int y = (rect. Height ()-cyicon + 1)/2;

// Draw the icon

DC. drawicon (X, Y, m_hicon );

}

Else

{

Cdialog: onpaint ();

}

}

// The system callthis to obtain the cursor to display while the user drags

// The minimized window.

Hcursor cjaystickdlg: onquerydragicon ()

{

Return (hcursor) m_hicon;

}

Lresult cjaystickdlg: windowproc (uint message, wparam, lparam)

{

Switch (Message ){

Case mm_joy1buttondown:

If (wparam & joy_button1 ){

Messagebeep (mb_iconquestion );

Setdlgitemtext (idc_button1, "button 1: <

Click> ");

}

If (wparam & joy_button2 ){

Messagebeep (mb_iconexclamation );

Setdlgitemtext (idc_button2, "button 2: <

Click> ");

}

Break;

Case mm_joy1buttonup:

If (wparam & joy_button1 ){

Setdlgitemtext (idc_button1, "button 1: <

Click> ");

Setdlgitemtext (idc_button2, "button 2 :");

}

Else if (wparam & joy_button2 ){

Setdlgitemtext (idc_button2, "button 2: <

Click> ");

Setdlgitemtext (idc_button1, "button 1 :");

}

Else {

Setdlgitemtext (idc_button1, "button 1 :");

Setdlgitemtext (idc_button2, "button 2 :");

}

Break;

Case mm_joy1move:

Word X, Y;

Point pt;

Updatedata (true );

//

Obtains the cursor position of the current screen.

Getcursorpos (& pt );

//

The coordinates of the game rod in the current system range from 0 to 65535. The position indicates that the range is from 0 to 31.

If (! M_absolute ){

X = loword (lparam)/2048;

Y = hiword (lparam)/2048;

//

If the player is on the left of the center, move the cursor to the left. And vice versa

If (x <= 12)

PT. x = pt. x + X-15;

Else if (x> = 20)

PT. x = pt. x + X-15;

//

Move the cursor to the bottom if the game rod is at the center. And vice versa

If (Y <= 12)

PT. Y = pt. Y + Y-15;

Else if (Y> = 20)

PT. Y = pt. Y + Y-15;

}

Else {

CDC * PDC = getdc ();

X = loword (lparam );

Y = hiword (lparam );

PT. x = (PDC-> getdevicecaps (horzres) * X)/65536;

PT. Y = (PDC-> getdevicecaps (vertres) * Y)/65536;

Releasedc (PDC );

}

/* Set the new cursor position .*/

Setcursorpos (Pt. X, Pt. y );

Break;

}

Return cdialog: windowproc (message, wparam, lparam );

}

Return cdialog: windowproc (message, wparam, lparam );

}

// Capture the joystick-JK

Int cjaystickdlg: oncreate (maid)

{

Int result;

If (cdialog: oncreate (lpcreatestruct) =-1)

Return-1;

Result = joysetcapture (cdialog: m_hwnd, joystickid1, 0, false );

If (result = joyerr_nocando ){

Messagebeep (mb_iconexclamation );

MessageBox ("

Cannot capture the game pole ", null, mb_ OK | mb_iconexclamation );

Return-1;

}

If (result = joyerr_unplugged ){

Messagebeep (mb_iconexclamation );

MessageBox ("players are not connected to the system", null, mb_ OK | mb_iconexclamation );

Return-1;

}

Return 0;

}

Void cjaystickdlg: ondestroy ()

{

Cdialog: ondestroy ();

Joyreleasecapture (joystickid1 );

}

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.