使用C++在console中獲取鼠標事件


第一點,使用頭文件<windows.h>

要在console里進行操作,首先先說一下ReadConsoleInput()函數。

msdn的具體解釋如下:

 

來自https://docs.microsoft.com/en-us/windows/console/readconsoleinput

這個函數主要是用於獲取控制台信息。

  • 第一個參數hConsoleInput是設備參數,可以通過GetStdHandle()返回設備句柄。
  • 第二個參數lpBuffer為設備中返回的信息,他是一個INPUT_RECORD的結構體組成的數組。
  • 第三個參數nLength為返回的信息里需要的相應的事件數。(文檔里寫的是返回信息的指針數組的長度)
  • 第四個參數LPWORD為返回已讀記錄數。

其實lpBuffer所返回的信息中,包括了多個事件,其中有FocusEvent,KeyEvent,WindowBufferSizeEvent,MouseEvent, MenuEvent多個事件。

我們這里引用鼠標事件,MouseEvent。而在lpBuffer中返回的lpBuffer.Event.MouseEvent.dwMousePosition就是鼠標在控制台界面里的所在位置。

 

左鍵單擊事件

判斷鼠標事件的話,就可以通過 lpBuffer.EventType == MOUSE_EVENT && lpBuffer.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED來判斷左鍵單擊事件。

左鍵雙擊事件

 lpBuffer.EventType == MOUSE_EVENT && lpBuffer.Event.MouseEvent.dwEventFlags == RIGHTMOST_BUTTON_PRESSED

右鍵單擊事件

 lpBuffer.EventType == MOUSE_EVENT && lpBuffer.Event.MouseEvent.dwButtonState == RIGHTMOST_BUTTON_PRESSED

其余的事件如下:

所以最后在程序中可以順利判斷各類鼠標狀態。

簡單的代碼例程。

/////////////////////////
// Writen by TianHuahua//
/////////////////////////

#include "stdafx.h"
#include "windows.h"
using namespace std; int main() { HANDLE ConsoleWin; INPUT_RECORD eventMsg; DWORD Pointer ; ConsoleWin = GetStdHandle(STD_INPUT_HANDLE);//Get the console window while(1){ ReadConsoleInput(ConsoleWin, &eventMsg, 1, &Pointer);//Read input msg if (eventMsg.EventType == MOUSE_EVENT && eventMsg.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED) { printf("Right button clicked."); } if (eventMsg.EventType == MOUSE_EVENT && eventMsg.Event.MouseEvent.dwEventFlags == RIGHTMOST_BUTTON_PRESSED) { printf("Left button double clicked."); } } return 0; }  

 

有一個問題需要注意:VS中編譯,在頭文件處,

#include "windows.h"
#include "stdafx.h" 

會出現類似錯誤。Handle:為聲明的標識符。

error C2146: 語法錯誤 : 缺少“;”(在標識符“hServStatus”的前面)
error C2501: “hServStatus” : 缺少存儲類或類型說明符
error C2146: 語法錯誤 : 缺少“;”(在標識符“hSStat”的前面)
error C2501: “hSStat” : 缺少存儲類或類型說明符
error C2065: “SERVICE_TABLE_ENTRY” : 未聲明的標識符
error C2146: 語法錯誤 : 缺少“;”(在標識符“DispatchTable”的前面)
error C2065: “DispatchTable” : 未聲明的標識符
error C2059: 語法錯誤 : “]”
error C2143: 語法錯誤 : 缺少“;”(在“{”的前面)
error C2143: 語法錯誤 : 缺少“;”(在“}”的前面)
warning C4550: 表達式計算為缺少參數列表的函數
error C2143: 語法錯誤 : 缺少“;”(在“,”的前面)
error C2143: 語法錯誤 : 缺少“;”(在“{”的前面)
error C2143: 語法錯誤 : 缺少“;”(在“}”的前面)
error C3861: “StartServiceCtrlDispatcher”: 即使使用參數相關的查找,也未找到標識符
error C3861: “DispatchTable”: 即使使用參數相關的查找,也未找到標識符
error C2065: “SC_HANDLE” : 未聲明的標識符
error C2146: 語法錯誤 : 缺少“;”(在標識符“schSCManager”的前面)
error C2065: “schSCManager” : 未聲明的標識符
error C2146: 語法錯誤 : 缺少“;”(在標識符“schService”的前面)
error C3861: “SC_HANDLE”: 即使使用參數相關的查找,也未找到標識符
error C2065: “schService” : 未聲明的標識符
error C2065: “SC_MANAGER_ALL_ACCESS” : 未聲明的標識符
error C3861: “schSCManager”: 即使使用參數相關的查找,也未找到標識符
error C3861: “OpenSCManager”: 即使使用參數相關的查找,也未找到標識符
error C3861: “schSCManager”: 即使使用參數相關的查找,也未找到標識符

解決方法:

改變頭文件的順序如下:

#include “stdafx.h”

#include <windows.h>

 

 

參考資料:

http://blog.csdn.net/bnb45/article/details/8042819

http://www.vcerror.com/?p=1944


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM