c++ - 通过鼠标单击获取二维数组的索引
问题描述
如果我在控制台中有一个二维数组,例如:
0123
0 OOOO
1 OOOO
2 OOOO
3 OOOX <--- mouse click here
我想用鼠标单击数组并获取数组的索引。例如,我单击位置 (3;3) 并在控制台中输出“x = 3 and y = 3”
我怎样才能在 C++ 中做到这一点?(我使用的是 Windows)
解决方案
试试这个代码:
#include <iostream>
#include <windows.h>
#include <stdio.h>
int main()
{
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hin = GetStdHandle(STD_INPUT_HANDLE);
INPUT_RECORD InputRecord;
DWORD Events;
COORD coord;
SetConsoleMode(hin, ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT);
while (1) {
ReadConsoleInput(hin, &InputRecord, 1, &Events);
switch (InputRecord.EventType) {
case MOUSE_EVENT: // mouse input
if (InputRecord.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED) //if mouse-1 is clicked
{
int x = InputRecord.Event.MouseEvent.dwMousePosition.X; //mouse coordinates
int y = InputRecord.Event.MouseEvent.dwMousePosition.Y;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(hout, coord); //sets vursors position for output
std::cout <<"x = " << x << " y = "<< y;
}
break;
}
}
}
这应该输出鼠标点击位置的坐标。
推荐阅读
- websocket - Websocket 连接 - 客户端尝试对一个地址而不是另一个地址进行 TLS 握手
- autoscaling - 如果 Spot 实例不适用于 AWS 自动扩展组,会发生什么情况?
- javascript - 在 Google Analytics(分析)中看到自定义跟踪代码管理器键?
- html - Thymeleaf Content-Type 在 HTML 电子邮件中被忽略(未呈现为 HTML)
- node.js - 通过 sequelize 中的第三个表连接两个表
- github - Python 库 PyGithub 在生产级项目中的可用性
- flutter - Flutter 将图像转换为二进制数据
- iis - 对于票务应用程序,iis 服务器中的连接超时值应该是多少
- android - 使用 GsonConverterFactory Retrofit 解析 json 的问题
- continuous-integration - GitHub Actions 运行 Espresso 测试