D3D鼠标输入处理

 

 

#include <windows.h>

#include <dinput.h>

#include <stdio.h>

 

 

#pragma    comment(lib,"dxguid.lib")

#pragma    comment(lib,"dinput8.lib")

#pragma    comment(lib,"odbc32.lib")

#pragma    comment(lib,"odbccp32.lib")

 

//定义全局变量

HINSTANCE g_hInst;

HWND g_hWnd;

const TCHAR g_szWndClass[]="Game";

const TCHAR g_szWndTitle[]="DirectInput鼠标输入";

WNDCLASSEX g_wcex;

 

extern long g_lMouseMoveX,g_lMouseMoveY;

 

 

//函数原型声明

ATOM RegWndClass(HINSTANCE);

BOOL CreateWnd(HINSTANCE, int);

LRESULT CALLBACK  WndProc(HWND, UINT, WPARAM, LPARAM);

extern BOOL InitMouse();

extern void CaptureMouse();

extern BOOL IsLButtonPressed();

extern BOOL IsRButtonPressed();

extern BOOL IsMButtonPressed();

extern void ReleaseCOMObject();

 

//函数定义

int APIENTRY WinMain(HINSTANCE hInstance,       //传入的窗口句柄

HINSTANCE hPrevInstance,   //已存在的窗口句柄

LPSTR     lpCmdLine,        //传入的命令行参数

int       nCmdShow)         //设置窗口的显示方式

{

MSG msg;

HDC hDC;

POINT cur_point;  //鼠标当前位置

g_hInst=hInstance;

if(!hPrevInstance){ //判断是否已有应用程序实例在运行

RegWndClass(hInstance);

}

if(!CreateWnd(hInstance, nCmdShow)){

return FALSE;

}

if(!InitMouse()){

return FALSE;

}

SetWindowPos(g_hWnd,0,0,0,0,0,SWP_NOSIZE);  //设置应用程序窗口位置

SetCursorPos(0,0);   //设置鼠标位置

hDC=GetDC(g_hWnd);

TCHAR tmpText[50];

 

while(msg.message!=WM_QUIT)

{

if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

else

{

GetCursorPos(&cur_point);  //取得鼠标的绝对屏幕位置

ScreenToClient(g_hWnd,&cur_point); //转换为窗口客户端的位置(除去标题栏高度)

CaptureMouse();

if(IsLButtonPressed())

{

sprintf(tmpText,"鼠标左键已按下,X-Y移动量分别为(%d,%d)",g_lMouseMoveX,g_lMouseMoveY);

TextOut(hDC,cur_point.x,cur_point.y,tmpText,lstrlen(tmpText));

}

if(IsMButtonPressed())

{

sprintf(tmpText,"鼠标滚轮已按下,X-Y移动量为(%d,%d)",g_lMouseMoveX,g_lMouseMoveY);

TextOut(hDC,cur_point.x,cur_point.y,tmpText,lstrlen(tmpText));

}

if(IsRButtonPressed())

{

sprintf(tmpText,"鼠标右键已按下,X-Y移动量为(%d,%d)",g_lMouseMoveX,g_lMouseMoveY);

TextOut(hDC,cur_point.x,cur_point.y,tmpText,lstrlen(tmpText));

}

 

}

}

ReleaseCOMObject();

UnregisterClass(g_szWndClass, g_wcex.hInstance);

return msg.wParam;

}

 

ATOM RegWndClass(HINSTANCE hInstance)

{

g_wcex.cbSize         = sizeof(WNDCLASSEX); 

g_wcex.style    = CS_HREDRAW | CS_VREDRAW;  //

g_wcex.lpfnWndProc = (WNDPROC)WndProc;

g_wcex.cbClsExtra = 0;

g_wcex.cbWndExtra = 0;

g_wcex.hInstance = hInstance;

g_wcex.hIcon    = 0;

g_wcex.hCursor = LoadCursor(NULL, IDC_ARROW);

g_wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);

g_wcex.lpszMenuName = 0;

g_wcex.lpszClassName = g_szWndClass;

g_wcex.hIconSm = LoadIcon(g_wcex.hInstance, (LPCTSTR)IDI_APPLICATION);

return RegisterClassEx(&g_wcex);

}

 

BOOL CreateWnd(HINSTANCE hInstance, int nCmdShow)

{

 

g_hWnd = CreateWindow(g_szWndClass, g_szWndTitle, WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!g_hWnd)

{

return FALSE;

}

ShowWindow(g_hWnd, nCmdShow);  //显示窗口

UpdateWindow(g_hWnd);  //刷新窗口

return TRUE;

}

 

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

switch (message)

{

case WM_KEYDOWN:

if(wParam==VK_ESCAPE)

{

PostQuitMessage(0);

}

break;  //防止程序按其他键后退出

case WM_DESTROY:

PostQuitMessage(0);

break;

default:

return DefWindowProc(hWnd, message, wParam, lParam);

}

return 0;

}

 

 


 

 

#include <dinput.h>

 

extern HINSTANCE g_hInst;

extern HWND g_hWnd;

#define SafeRelease(pObject) if(pObject != NULL) {pObject->Release(); pObject=NULL;}

#define ITEMS_NUM 10  //缓冲区的数组元素个数

LPDIRECTINPUT8 pIDirectInput=NULL;

LPDIRECTINPUTDEVICE8 pIDirectInputDevice=NULL;

DIDEVICEOBJECTDATA diDeviceObjectData[ITEMS_NUM];

long g_lMouseMoveX=0;  //鼠标X轴的总移动量

long g_lMouseMoveY=0;  //鼠标Y轴的总移动量

BOOL InitMouse();      //创建DirectInput鼠标设备

void CaptureMouse();   //读取鼠标输入数据

BOOL IsLButtonPressed();  //判断鼠标左键按下

BOOL IsRButtonPressed();  //判断鼠标右键按下

BOOL IsMButtonPressed();  //判断鼠标滚轮按下

void ReleaseCOMObject();


#include "HandleMouse.h"
BOOL InitMouse()
{
HRESULT hr;
//创建IDirectInput接口对象
hr=DirectInput8Create(g_hInst,DIRECTINPUT_VERSION,IID_IDirectInput8,(void**)&pIDirectInput,NULL);
if(FAILED(hr))
{
MessageBox(NULL,"建立IDIRECTINPUT8接口对象失败.","警告",MB_OK|MB_ICONINFORMATION);
return false;
}
//鼠标输入设备
hr=pIDirectInput->CreateDevice(GUID_SysMouse,&pIDirectInputDevice,NULL);//GUID_SysMouse
if(FAILED(hr))
{
MessageBox(NULL,"建立IDIRECTINPUTDEVICE8鼠标输入设备对象失败.","警告",MB_OK|MB_ICONINFORMATION);
SafeRelease(pIDirectInput);
return false;
}
//设置鼠标设备的数据格式
hr=pIDirectInputDevice->SetDataFormat(&c_dfDIMouse);
if(FAILED(hr))
{
MessageBox(NULL,"设置鼠标的数据读取格式失败.","警告",MB_OK|MB_ICONINFORMATION);
SafeRelease(pIDirectInputDevice);
SafeRelease(pIDirectInput);
return false;
}
//设置鼠标设备的协调级别
hr=pIDirectInputDevice->SetCooperativeLevel(g_hWnd,DISCL_FOREGROUND|DISCL_NONEXCLUSIVE);
if(FAILED(hr))
{
MessageBox(NULL,"设置协调级别失败.","警告",MB_OK|MB_ICONINFORMATION);
SafeRelease(pIDirectInputDevice);
SafeRelease(pIDirectInput);
return false;
}
//设置鼠标设备的属性(使用缓冲区读数据)
DIPROPDWORD dipROPWORD;
dipROPWORD.diph.dwSize=sizeof(DIPROPDWORD);
dipROPWORD.diph.dwHeaderSize=sizeof(DIPROPHEADER);
dipROPWORD.diph.dwObj=0;
dipROPWORD.diph.dwHow=DIPH_DEVICE;
dipROPWORD.dwData=ITEMS_NUM;  //#define ITEMS_NUM 10
if(FAILED(pIDirectInputDevice->SetProperty(DIPROP_BUFFERSIZE,&dipROPWORD.diph)))
{
MessageBox(NULL,"设置鼠标设备属性失败.","警告",MB_OK|MB_ICONINFORMATION);
SafeRelease(pIDirectInputDevice);
SafeRelease(pIDirectInput);
return false;
}
//获取鼠标设备的访问权
hr=pIDirectInputDevice->Acquire();
if(FAILED(hr))
{
MessageBox(NULL,"取得鼠标的访问权失败.","警告",MB_OK|MB_ICONINFORMATION);
SafeRelease(pIDirectInputDevice);
SafeRelease(pIDirectInput);
return false;
}
return true;
}
void CaptureMouse()
{
HRESULT hr;
DWORD dwReadNum=1;
int i;
//一定要使鼠标缓冲区清零
ZeroMemory(diDeviceObjectData,sizeof(DIDEVICEOBJECTDATA)*ITEMS_NUM);
//循环读取鼠标数据
for(i=0;i<ITEMS_NUM;i++)
{
hr=pIDirectInputDevice->GetDeviceData(sizeof(DIDEVICEOBJECTDATA),&diDeviceObjectData[i],&dwReadNum,0);
if(hr==DIERR_INPUTLOST)
{
pIDirectInputDevice->Acquire();
hr=pIDirectInputDevice->GetDeviceData(sizeof(DIDEVICEOBJECTDATA),&diDeviceObjectData[i],&dwReadNum,0);
if(FAILED(hr)){
MessageBox(NULL,"取得鼠标缓冲区数据失败.","警告",MB_OK|MB_ICONINFORMATION);
return;
}
}
if(diDeviceObjectData[i].dwOfs==DIMOFS_X)
{
g_lMouseMoveX+=diDeviceObjectData[i].dwData;
}
if(diDeviceObjectData[i].dwOfs==DIMOFS_Y)
{
g_lMouseMoveY+=diDeviceObjectData[i].dwData;
}
}
}
//判断鼠标左键按下
BOOL IsLButtonPressed()
{
for(int i=0;i<ITEMS_NUM;i++)
{
if((diDeviceObjectData[i].dwOfs==DIMOFS_BUTTON0) && (diDeviceObjectData[i].dwData & 0x80))
{
return true;
}
}
return false;
}
//判断鼠标右键按下
BOOL IsRButtonPressed()
{
for(int i=0;i<ITEMS_NUM;i++)
{
if((diDeviceObjectData[i].dwOfs==DIMOFS_BUTTON1) && (diDeviceObjectData[i].dwData & 0x80))
{
return true;
}
}
return false;
}
//判断鼠标滚轮按下
BOOL IsMButtonPressed()
{
for(int i=0;i<ITEMS_NUM;i++)
{
if((diDeviceObjectData[i].dwOfs==DIMOFS_BUTTON2) && (diDeviceObjectData[i].dwData & 0x80))
{
return true;
}
}
return false;
}
void ReleaseCOMObject()
{
if(pIDirectInputDevice)
pIDirectInputDevice->Unacquire();   //释放DirectInput设备的使用权
    SafeRelease(pIDirectInputDevice);
SafeRelease(pIDirectInput);
}

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值