1.#include "Mmsystem.h"
2.加入winmm.lib
3.重载WM_CREATE
int CMyView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CListView::OnCreate(lpCreateStruct) == -1)
return -1;
int result=::joySetCapture(CMyView::m_hWnd,JOYSTICKID1,0,FALSE);
if(result==JOYERR_NOCANDO)
{
MessageBeep(MB_ICONEXCLAMATION);
MessageBox("不能捕获游戏杆", NULL, MB_OK | MB_ICONEXCLAMATION);
}
if(result==JOYERR_UNPLUGGED)
{
MessageBeep(MB_ICONEXCLAMATION);
MessageBox("游戏杆未与系统连接", NULL, MB_OK | MB_ICONEXCLAMATION);
}
// TODO: Add your specialized creation code here
return 0;
}
4.重载WindowProc
LRESULT CMyView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
WORD x,y;
switch(message)
{
case MM_JOY1BUTTONDOWN :
if (wParam & JOY_BUTTON1)//如果按下按钮一
CMyView::MessageBox("1 down",NULL,MB_OK);
break;
case MM_JOY1MOVE :
UpdateData(TRUE);
x = LOWORD(lParam)/2048;
y = HIWORD(lParam)/2048;
if (x<15) ;//按下左键
if (x>15) ;//按下右键
if (y<15) ;//按下上键
if (y>15) ;//按下下键
break;
default:
break;
}
return CListView::WindowProc(message, wParam, lParam);
}
5.重载WM_CREATE
void CMyView::OnDestroy()
{
::joyReleaseCapture(JOYSTICKID1);
CListView::OnDestroy();
// TODO: Add your message handler code here
}