1.3 Keyboard & Mouse之封装CXKeyboard & CXMouse
[
http://blog.csdn.net/mythma]1、 何为Keyboard & Mouse
“地球人都知道”。DX9提供的接口 IDirectInputDevice8。
2、 二者的功能
Keyboard:读取键盘的按键信息
Mouse:读取鼠标的按键、位置信息,设置光标属性(如用图片表示光标)。
3、 使用过程
创建 IDirectInput8 对象 DirectInput8Create
创建 IDirectInput8 设备(键盘、鼠标等) CreateDevice
设置设备属性 SetCooperativeLevel
SetDataFormat
获取设备使用权 Acquire
读取设备传入的数据 GetDeviceState
释放设备及 IDirectInput8 Release
设置鼠标光标过程:
创建光标图片资源: 参照surfaces
设置光标为指定的图片 SetCursorProperties
设置光标的初始位置 SetCursorPosition
显示光标 ShowCursor
程序段如下:
LPDIRECTINPUT8 g_lpDI;
LPDIRECTINPUTDEVICE8 g_Keyboard;
LPDIRECTINPUTDEVICE8 g_Mouse
//========================================================
HRESULT Result = DirectInput8Create(g_hInst, DIRECTINPUT_VERSION,
IID_IDirectInput8, (void**)&g_lpDI, NULL);
if(SUCCEEDED(Result))
{
//创建键盘设备
Result = g_lpDI->CreateDevice(GUID_SysKeyboard, &g_lpDIDevice, NULL);
if(SUCCEEDED(Result))
{
g_lpDIDevice->SetCooperativeLevel(g_hWnd, DISCL_FOREGROUND |
DISCL_NONEXCLUSIVE);
g_lpDIDevice->SetDataFormat(&c_dfDIKeyboard);
}
}
//-------------------------------------------------------------------------------------------
//获取按键信息
if(SUCCEEDED(g_Keyboard->Acquire())) //Acquire the device
{
char KeyState[256];
g_Keyboard->GetDeviceState(sizeof(KeyState),(LPVOID)&KeyState);
//根据KeyState返回的数据就可以判断按下何键
}
//====================================================
//创建鼠标
g_lpDI->CreateDevice(GUID_SysMouse, &g_Mouse, NULL);
g_Mouse->SetDataFormat(&c_dfDIMouse);
g_Mouse->SetCooperativeLevel(g_hWnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND);
//------------------------------------------------------------------------
//设置鼠标光标
//获取光标图片
D3DXIMAGE_INFO ImageInfo;
D3DXGetImageInfoFromFile("C:\\Cursor.jpg”, &ImageInfo);
g_pd3dDevice->CreateOffscreenPlainSurface(ImageInfo.Width,
ImageInfo.Height,
ImageInfo.Format,
D3DPOOL_DEFAULT,
&g_MouseCursor,
NULL);
D3DXLoadSurfaceFromFile(g_MouseCursor, NULL, NULL, ("C:\\Cursor.jpg”,
NULL, D3DX_FILTER_NONE, 0xFF000000, NULL);
//设置成指定的光标
g_pd3dDevice->SetCursorProperties(0,0, g_MouseCursor);
//初试位置
g_pd3dDevice->SetCursorPosition(0,0,D3DCURSOR_IMMEDIATE_UPDATE);
//显示光标
g_pd3dDevice->ShowCursor(true);
//-------------------------------------------------------------------------
//获取鼠标信息
if(SUCCEEDED(g_Mouse->Acquire()))
{
DIMOUSESTATE State;
g_Mouse->GetDeviceState(sizeof(DIMOUSESTATE),(LPVOID)&State);
//信息保存在 State中。
}
//==============================================
//释放设备
if(g_lpDI != NULL)
g_lpDI->Release();
4、 封装
CXKeyboard的封装
class CXKeyboard
{
private:
LPDIRECTINPUTDEVICE8 m_pDevice;
char m_KeyState[256];
public:
CXKeyboard (LPDIRECTINPUT8 pInput, HWND hWnd);
~ CXKeyboard();
bool IsKeyPressed(int Key);
HRESULT Update();
};
CXMouseSurface的封装
为了能制作出动画效果的光标,因此需要一个surfaces列表,因此从CXSurface继承一个CXMouseSurface类,使之能够操作多个图片。
class CXMouseSurface : public CXSurface
{
private:
CXMouseSurface* m_pNext;
int MouseSurfaceType;
UINT HotSpotX;
UINT HotSpotY;
public:
CXMouseSurface* GetNext() {return m_pNext;}
void SetNext(CXMouseSurface* Surf) {m_pNext = Surf;}
int GetSurfaceType() {return MouseSurfaceType;}
void SetSurfaceType(int Type) {MouseSurfaceType = Type;}
UINT GetHotSpotX() {return HotSpotX;}
UINT GetHotSpotY() {return HotSpotY;}
void SetHotSpotX (UINT X) {HotSpotX = X;}
void SetHotSpotY (UINT Y) {HotSpotY = Y;}
CXMouseSurface(LPDIRECT3DDEVICE9 pDevice) : CXSurface(pDevice)
{m_pNext = NULL;}
CXMouseSurface() : CXSurface() {m_pNext = NULL;}
};
CXMouse的封装
class CXMouse
{
private:
CXMouseSurface* m_Surface; //指向光标列表
CXMouseSurface* m_CurrentCursorSurface; //当前光标
LPDIRECTINPUTDEVICE8 m_pDevice;
LPDIRECT3DDEVICE9 m_p3DDevice;
DIMOUSESTATE m_State;
LONG m_iX;
LONG m_iY;
public:
CXMouse (LPDIRECT3DDEVICE9 pDevice, LPDIRECTINPUT8 pInput, HWND hWnd,
bool Exclusive);
~ CXMouse();
HRESULT Update();
LONG GetXPos();
LONG GetYPos();
bool IsButtonPressed(int Button);
HRESULT SetCursorImage();
HRESULT SetMouseCursor(char* FilePath, UINT HotSpotX, UINT HotSpotY,
int Type);
void AddCursorSurface(CXMouseSurface* Surface);
CXMouseSurface* GetFirstSurface() {return m_Surface;}
bool SetCursor(int Type);
CXMouseSurface* GetCurrentCursor() {return m_CurrentCursorSurface;}
void SetCurrentCursor(CXMouseSurface* Surface)
{m_CurrentCursorSurface = Surface;}
void SetCursorPosition(int X, int Y);
HRESULT SetCursorVisible(bool Show);
};