在使用vc2003进行DirectInput编程,其中DirectX SDK使用的是系统默认的版本8.0,操作系统环境为WINXP SP2.
在进行编程中发现并解决了以下问题:
1、库文件及头文件
在vc2003中要加入头文件#i nclude
还有库文件应该是dinput8.lib ,而不是dinput.lib
2、变量的声明,如
LPDIRECTINPUT8 pDI;
LPDIRECTINPUTDEVICE8 lpMouse;
3、DirectInput的初始化
bool InitInput()
{
HRESULT hres;
hres=DirectInput8Create(hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (LPVOID *)&pDI, NULL);
if(FAILED(hres))
return FALSE;
hres = pDI->CreateDevice(GUID_SysMouse, &lpMouse,NULL);
if(FAILED(hres))
return FALSE;
hres = lpMouse->SetDataFormat(&c_dfDIMouse);
if(FAILED(hres))
return FALSE;
hres = lpMouse->SetCooperativeLevel(hWnd,DISCL_EXCLUSIVE | DISCL_FOREGROUND);
if(FAILED(hres))
return FALSE;
hMouseEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if(!hMouseEvent)
return FALSE;
hres = lpMouse->SetEventNotification(hMouseEvent);
if(FAILED(hres))
return FALSE;
DIPROPDWORD dipdw;
dipdw.diph.dwSize = sizeof(DIPROPDWORD);
dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
//dipdw.dwData=MOUSE_SAMPLEBUFFER; // 预定义为16
dipdw.dwData=16; // 预定义为16
hres = lpMouse->SetProperty(DIPROP_BUFFERSIZE,&dipdw.diph);
if(FAILED(hres))
return FALSE;
lpMouse->Acquire();
return TRUE;
}
4、使用鼠标
void MouseEvent()
{
DIDEVICEOBJECTDATA od;
HRESULT hres;
DWORD count;
short x = 0,y = 0;
while(1)
{
count = 1;
hres =lpMouse->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), &od,&count, 0);
if(hres == DIERR_INPUTLOST)
{
lpMouse->Acquire();
return;
}
if(FAILED(hres) || !count)
break;
switch(od.dwOfs)
{
case DIMOFS_X:
x += (short)od.dwData;
break;
case DIMOFS_Y:
y += (short)od.dwData;
break;
//物理设备上左键或右键按下/释放,如有左右键交换可是要自己判断的
case DIMOFS_BUTTON0:
MessageBox(hWnd,"abcdfsfds","df",0);
case DIMOFS_BUTTON1:
//if(od.dwData & 0x80)
// 键按下
//…………
//else
// 键释放
//…………
; }
}
if(x || y)
{
//MouseX += x;
//MouseY += y;
// 决定光标的位置以及是否卷屏等等
//…………
//lpDevice->SetCursorPosition(MouseX, MouseY,D3DCURSOR_IMMEDIATE_UPDATE);
}
}
阅读(2904) | 评论(1) | 转发(0) |