dxmouse.cpp
上传用户:henghua
上传日期:2007-11-14
资源大小:7655k
文件大小:3k
- #include "dxmouse.h"
- bool dxmouse::mousedown(int button)
- {
- return mouseState.rgbButtons[button] & 0x80;
- }
- // 初始化创建及设置,并获得鼠标输入状态
- bool dxmouse::Init(void)
- {
- HRESULT hr;
- // 创建Direct输入对象
- hr= (DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&lpdi, NULL));
- if(FAILED(hr))
- {
- MessageBox(hWnd, "Could not create main DInput object", "ERROR", MB_OK);
- return false;
- }
- // 创建键盘输入设备
- hr= (lpdi->CreateDevice(GUID_SysKeyboard, &lpdiKeyboard, NULL));
- if(FAILED(hr))
- {
- MessageBox(hWnd, "Could not create keyboard's object", "ERROR",MB_OK);
- Shutdown();
- return false;
- }
- // 设置键盘数据格式
- hr= (lpdiKeyboard->SetDataFormat(&c_dfDIKeyboard));
- if(FAILED(hr))
- {
- MessageBox(hWnd, "Could not set keyboard's data format","ERROR",MB_OK);
- Shutdown();
- return false;
- }
- hr= (lpdiKeyboard->SetCooperativeLevel(hWnd, DISCL_BACKGROUND|DISCL_NONEXCLUSIVE));
- if(FAILED(hr))
- {
- MessageBox(hWnd, "Could not set keyboard's cooperation level", "ERROR", MB_OK);
- Shutdown();
- return false;
- }
- lpdiKeyboard->Acquire();
- // 创建鼠标输入设备
- hr= lpdi->CreateDevice(GUID_SysMouse, &lpdiMouse, NULL);
- if(FAILED(hr))
- {
- MessageBox(hWnd, "Could not set create the mouse device", "ERROR", MB_OK);
- Shutdown();
- return FALSE;
- }
- // 设置鼠标数据格式
- hr= lpdiMouse->SetDataFormat(&c_dfDIMouse);
- if(FAILED(hr))
- {
- MessageBox(hWnd, "Could not set the mouse's data format", "ERROR", MB_OK);
- Shutdown();
- return FALSE;
- }
- hr= lpdiMouse->SetCooperativeLevel(hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
- if(FAILED(hr))
- {
- MessageBox(hWnd, "Could not set the mouse's behavior", "ERROR", MB_OK);
- Shutdown();
- return FALSE;
- }
- hr= lpdiMouse->Acquire();
- if(FAILED(hr))
- {
- MessageBox(hWnd, "Could not acquire the mouse", "ERROR", MB_OK);
- Shutdown();
- }
- // 获得鼠标设备状态
- hr= lpdiMouse->GetDeviceState(sizeof(DIMOUSESTATE), &mouseState);
- if(FAILED(hr))
- {
- MessageBox(hWnd, "The mouse has been lost in initialization", "ERROR", MB_OK);
- Shutdown();
- }
- return true;
- }
-
-
- // 遇到错误调用
- void dxmouse::Shutdown(void)
- {
- if(lpdiMouse!=NULL)
- {
- lpdiMouse->Unacquire();
- lpdiMouse->Release();
- lpdiMouse= NULL;
- }
- if(lpdiKeyboard!=NULL)
- {
- lpdiKeyboard->Unacquire();
- lpdiKeyboard->Release();
- lpdiKeyboard= NULL;
- }
-
- if(lpdi!=NULL)
- {
- lpdi->Release();
- lpdi=NULL;
- }
- }
- // 更新信息
- void dxmouse::Update(void)
- {
- HRESULT hr;
-
- // 检测键盘设备,若丢失,重找回
- hr= (lpdiKeyboard->GetDeviceState(sizeof(UCHAR[256]),(LPVOID)&keyBuffer));
- if(FAILED(hr))
- {
- if(hr==DIERR_INPUTLOST)
- {
- hr= (lpdiKeyboard->Acquire());
- if(FAILED(hr))
- {
- MessageBox(hWnd, "Keyboard has been lost", "ERROR", MB_OK);
- Shutdown();
- }
- }
- }
- // 检测鼠标设备,若丢失,重找回
- hr= (lpdiMouse->GetDeviceState(sizeof(DIMOUSESTATE),(LPVOID)&mouseState));
- if(FAILED(hr))
- {
- if(hr==DIERR_INPUTLOST)
- {
- hr= (lpdiMouse->Acquire());
- if(FAILED(hr))
- {
- MessageBox(hWnd, "The mouse has been lost", "ERROR", MB_OK);
- Shutdown();
- }
- }
- }
- // 得到有关鼠标的信息
- GetWheelMovement();
- GetMouseMovement();
- }
- void dxmouse::GetWheelMovement(void)
- {
- z= mouseState.lZ;
- }
- void dxmouse::GetMouseMovement(void)
- {
- x= mouseState.lX;
- y= mouseState.lY;
- }