#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();