谁帮我看下代码 鼠标输入
#include<Windows.h>#include<d3d9.h>
#include<d3dx9.h>
#include<iostream>
#include <dinput.h>
using namespace std;
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
#pragma comment(lib,"dinput8.lib")
#pragma comment(lib,"dxguid.lib")
#define KEY_DOWN(vk_code)((GetAsyncKeyState(vk_code)&0x8000)?1:0)
const string APPTITLE = "XInput Test Program";
const int SCREENW = 640;
const int SCREENH = 480;
LPDIRECT3D9 d3d = NULL;
LPDIRECT3DDEVICE9 d3ddev = NULL;
LPDIRECTINPUT8 lpdi = NULL;
LPDIRECTINPUTDEVICE8 dimouse = NULL;
DIMOUSESTATE mouse_state;
bool gameover = false;
bool Game_Init(HWND hwnd)
{
if (lpdi->CreateDevice(GUID_SysMouse, &dimouse, NULL) != DI_OK)
return(0);
//设置鼠标协作等级,设置为后台/非独占模式
if (dimouse->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND) != DI_OK)
return(0);
//设置数据格式,鼠标对应的数据格式为c_dfDIMouse
if (dimouse->SetDataFormat(&c_dfDIMouse) != DI_OK)
return(0);
//获取鼠标
if (dimouse->Acquire() != DI_OK)
return(0);
}
void Game_Run(HWND hwnd)
{
//读取鼠标数据
if (dimouse)
{
if (dimouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&mouse_state) != DI_OK)
return;
}
else
{
//如果没有插入鼠标,则将鼠标数据结构置为0
memset(&mouse_state, 0, sizeof(mouse_state));
return;
}
return;
}
void Game_End(HWND hwnd)
{
if (d3ddev) d3ddev->Release();
if (d3d)d3d->Release();
if (dimouse)dimouse->Release();
}
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
gameover = true;
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hIcon = NULL;
wc.hIconSm = NULL;
wc.lpszMenuName = NULL;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszClassName = "MainWindowClass";
if (!RegisterClassEx(&wc))
return FALSE;
HWND window = CreateWindow("MainWindowClass", APPTITLE.c_str(), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, SCREENW, SCREENH, NULL, NULL, hInstance, NULL);
if (window == 0) return 0;
ShowWindow(window, nCmdShow);
UpdateWindow(window);
if (!Game_Init(window))return 0;
MSG message;
while (!gameover)
{
if (PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
Game_Run(window);
if (mouse_state.rgbButtons[0] & 0x80) //左键是否按下
{
int i = 0;
MessageBox(window, "ok", "ok", 0);
}
//mouse_state.lX; ///鼠标横向移动的距离
//mouse_state.lY; ///鼠标纵向移动的距离
//mouse_state.rgbButtons[0]; ///鼠标左键是否按下
//mouse_state.rgbButtons[1]; ///鼠标右键是否按下
//mouse_state.rgbButtons[2]; ///鼠标中键是否按下
}
Game_End(window);
return message.wParam;
}