D3D滑鼠輸入處理

來源:互聯網
上載者:User

 

 

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

 

 

//函數原型聲明

ATOMRegWndClass(HINSTANCE);

BOOLCreateWnd(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_SysMouseif(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 10if(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);}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.