Programming for the game rod with VC5.0

zhaozj2021-02-11  183

Programming for the game rod with VC5.0

Liu Li Tianb

There is a JoyStickProperties control panel in Windows95 / 98. As long as you have a sound card that supports the Joystick connection, you can enjoy the powerful features it provides.

Here is how to use Visual C to achieve programming for the game rod, hoping to help everyone.

One capture and release jam

So how do you use Visual C 5.0 to program the game rod? First, you must capture the joystick. Here we use the JoySetCapture function to achieve this. The following is the definition of the JoySetCapture function and a description of each of its parameters.

INT JOYSETCAPTURE (HWND HWND, UNIT UJOYID, UNIT Uperiod, Bool Fchanged);

[1] hwnd ----- the parent window handle.

[2] ujoyid ---- Specify the joystick lever, which can be JoyStickID1 or JoyStickID2.

[3] Uperiod ---- Sends information about the joystick to the application every given polling interval. This parameter

It is a polling frequency in subtle.

The following is the program code for capturing the game pole message.

int CJaystickDlg :: OnCreate (LPCREATESTRUCT lpCreateStruct) {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 rod", NULL, MB_OK | MB_ICONEXCLAMATION;

Return -1;

} if (result == joyerr_unplugged) {messagebeep (mb_iconexclamation);

MessageBox ("Game Pole is not connected to system connection", NULL, MB_OK | MB_ICONEXCLAMATION;

Return -1;

} Return 0;}

Once start capturing the joystick message, you can check the message returned from the device. In order to capture the joystick news, we have written a WindowProc process, and the list of procedures is as follows:

LRESULT CJAYSTICKDLG :: WindowProc (uint message, wparam wparam, lparam lparam) {switch {copy mm_joy1buttondown: if (wparam & joy_button1) {messagebeep (MB_ICONQUESTION);

Setdlgitemtext (IDC_Button1, "Button 1: << button Press >>");

} IF (wPARAM & JOY_BUTTON2) {MessageBeep (MB_ICONEXCLAMATION);

Setdlgitemtext (IDC_Button2, "Button 2: << button Press >>");

} Break; Case MM_JOY1BUTTONUP: IF (WPARAM & JOY_BUTTON1) {

Setdlgitemtext (IDC_Button1, "Button 1: << button Press >>");

Setdlgitemtext (IDC_Button2, "Button 2:");} else if (wparam & joy_button2) {setdlgitemtext (IDC_Button2, "Button 2: << button Press >>");

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);

/ / Get the current screen cursor position

GetCursorpos (& PT);

// The position coordinate range of the game rod in the current system ranges from 0 65535. Location representation ranges from 0-31

IF (! m_absolute) {x = loword (lparam) / 2048; y = HiWord (lparam) / 2048;

// If the joystick is on the left side of the center position, move the cursor to the left. vice versa

IF (x <= 12) pt.x = pt.x x - 15; Else IF (x> = 20) Pt.x = Pt.x x - 15;

// If the joystick is in the lower side of the center position, move the cursor to the lower side. 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);

Take the player device message in the WindowProc process. MM_JOY1BUTTONUP, MM_JOY1BUTTONUP, MM_JOY1MOVE. WindowEProc parameters WPARAM Save the game lever button status information, parameter iParam's high hexadecimal Y coordinate (0 65 535), low sixteen saved X coordinates (0 65 535). Location (0, 0) represents the upper left corner of the game lever position, the position (65 535, 65 535) represents the lower right corner of the bit rod position.

Use the game pole to ensure release. Implemented with a JoyreleaseCapture function. The following is the definition of this function:

MmResult JoyreleaseCapture (Unit ujoyid);

Where ujoyid is one of JoystickID1 and JoyStickID2. The following program list shows how the game lever is released in the DESTROYWINDOW event of the application.

Void cjaystickdlg :: overdestroy () {cdialog :: overdestroy (); joyreleasecapture (joystickid1);}

Three application examples

In conjunction with the methods described herein, a simple application has been written. This app creates a simple dialog window. After starting the application, you will appear next to the button text whenever you press a button on the joystick. When moving the game rod, the cursor of the mouse will move on the screen. When you release the game rod, the cursor of the mouse remains unchanged at the current position. figure 1

In Visual C 5.0, the dialog box program framework is generated with AppWizard. The dialog is shown in Figure 1. We only need to edit JOYSTDLG.CPP and you can implement it. The full source code for JOYSTDLG.CPP is given below.

// 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 {

1/4 1/4}

/ // 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 SE ndMessage (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 calls this 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 wParam, LPARAM lParam) {switch (message) {case MM_JOY1BUTTONDOWN: if (wParam & JOY_BUTTON1) {MessageBeep (MB_ICONQUESTION); SetDlgItemText (IDC_BUTTON1, "Button 1: << pressing the button> > ");} if (wParam & JOY_BUTTON2) {MessageBeep (MB_ICONEXCLAMATION); SetDlgItemText (IDC_BUTTON2," button 2: << button is pressed >> ");} break; case MM_JOY1BUTTONUP: if (wParam & JOY_BUTTON1) {SetDlgItemText ( IDC_Button1, "Button 1: << button Press >>"); setdlgitemtext (IDC_Button2, "Button 2:");} else if (wparam & joy_button2) {setdlgitemtext (IDC_Button2, "Button 2: << button Press> > "); 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); // Get the current screen cursor position getCursorpos (& PT); // The position coordinate range of the game rod in the current system ranges from 0 65535. Location representation ranges from 0-31 if (! M_absolute) {x = loword (lparam) / 2048; y = HiWord (LPARAM) / 2048; // If the joystick is on the left side of the center position, the cursor is moved to the left. Vioence IF (x <= 12) pt.x = pt.x x - 15; Else IF (x> = 20) Pt.x = Pt.x x - 15; // If the joystick is in the center position The lower side, moving the cursor to the lower side. Vioeor 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;} returnig :: WindowProc (message, wparam, lparam);} Return CDIALOG: : WindowProc (Message, WPARAM, LPARAM);} // Capture The Joystick - JK

int CJaystickDlg :: OnCreate (LPCREATESTRUCT lpCreateStruct) {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 ( "not capture joystick", NULL, MB_OK | MB_ICONEXCLAMATION); return -1;} if (result == JOYERR_UNPLUGGED) {MessageBeep (MB_ICONEXCLAMATION); MessageBox ( "joystick is not }R} v (); joyreleasecapture (); joyreleasecapture (); joyreleasecapture (); joyreleasecapture (); joyreleasecapture (joystickid1);

转载请注明原文地址:https://www.9cbs.com/read-4091.html

New Post(0)