• 使用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

  • 相关阅读:
    如何使用pip安装PythonMySQLdb模块?
    Linux:信号(1):signal函数、pause函数、alarm函数
    python字符串前面加上'r'的作用
    在LINUX中 用Ctrl+z挂起的命令怎么切回到原任务的命令窗口
    A*寻路初探 GameDev.net
    [3d跑酷] Xcode5 打包 发布配置
    [cb]NGUI组件基类之 UIWidget
    [cb]Unity 项目架构
    使用Unity3D的50个技巧:Unity3D最佳实践
    Doxygen Tool For Unity
  • 原文地址:https://www.cnblogs.com/TIANHUAHUA/p/7805687.html
Copyright © 2020-2023  润新知