如果我在控制台中有一个二维数组,例如:
having
我想用鼠标单击该数组并获取该数组的索引。例如,我单击位置(3; 3),它在控制台“ x = 3和y = 3”中输出
如何在C ++中做到这一点? (我使用的是Windows)
答案 0 :(得分:1)
尝试以下代码:
#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;
}
}
}
这应该输出鼠标单击位置的坐标。