DirectInput输入——鼠标设备

//主DInput COM对象
LPDIRECTINPUT8 lpdi = NULL;
//键盘设备
LPDIRECTINPUTDEVICE8 lpdimouse = NULL;
//获取的鼠标数据放在此结构中
DIMOUSESTATE mouse_state; 

int DInput_Init_Mouse(void);
int DInput_Read_Mouse(void);
void DInput_Release_Mouse(void);

//初始化鼠标设备
int DInput_Init_Mouse(void)
{
	//创建鼠标设备
	if (lpdi->CreateDevice(GUID_SysMouse, &lpdimouse, NULL)!=DI_OK)
		return(0);

	//设置鼠标协作等级,设置为后台/非独占模式
	if (lpdimouse->SetCooperativeLevel(main_window_handle, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)!=DI_OK)
		return(0);

	//设置数据格式,鼠标对应的数据格式为c_dfDIMouse
	if (lpdimouse->SetDataFormat(&c_dfDIMouse)!=DI_OK)
		return(0);

	//获取鼠标
	if (lpdimouse->Acquire()!=DI_OK)
		return(0);
}

//从鼠标读取数据
int DInput_Read_Mouse(void)
{
	//读取鼠标数据
	if (lpdimouse)    
	{
		if (lpdimouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouse_state)!=DI_OK)
			return(0);
	}
	else
	{
		//如果没有插入鼠标,则将鼠标数据结构置为0
		memset(&mouse_state,0,sizeof(mouse_state));
		return(0);
	}
	return(1);
}

//释放鼠标
void DInput_Release_Mouse(void)
{
	if (lpdimouse)
	{    
		lpdimouse->Unacquire();
		lpdimouse->Release();
	} 
} 

	//主Game循环中
	DInput_Read_Mouse();
	if(mouse_state.rgbButtons[0] & 0x80) //左键是否按下
	{
		int i = 0;
	}
	mouse_state.lx;				///鼠标横向移动的距离
	mouse_state.ly;				///鼠标纵向移动的距离
	mouse_state.rgbButtons[0];	///鼠标左键是否按下
	mouse_state.rgbButtons[1];	///鼠标右键是否按下
	mouse_state.rgbButtons[2];	///鼠标中键是否按下

posted @ 2011-01-27 17:39  斯芬克斯  阅读(3852)  评论(0编辑  收藏  举报