C程序可以让字符鼠标悬停在Windows控制台中吗?

时间:2018-05-23 20:40:55

标签: c get console character mouseover

有关于鼠标输入的问题和答案,但鼠标输入似乎没有解决这个问题。

让我们说控制台上印有“椅子”这个词。有没有办法让鼠标悬停在哪个角色('c','h','a','i','r'或无)?

我真的不需要这个功能来不断更新自己,我只需要学习一次这个角色(就像用户按下按钮一样)。

1 个答案:

答案 0 :(得分:0)

最简单的事情可能是track mouse movement with ReadConsoleInput,它将为您提供鼠标指针的控制台屏幕缓冲区坐标,然后使用ReadConsoleOutputCharacter获取字符,如:

INPUT_RECORD irInBuf[128];
DWORD cNumRead;
int i;
COORD coord;

while (1)
{
    ReadConsoleInput(
        GetStdHandle(STD_INPUT_HANDLE),      // input buffer handle 
        irInBuf,     // buffer to read into 
        128,         // size of read buffer 
        &cNumRead);
    for (i = 0; i < cNumRead; i++)
    {
        if (irInBuf[i].EventType == MOUSE_EVENT)
        {
            MOUSE_EVENT_RECORD mer = irInBuf[i].Event.MouseEvent;
            switch (mer.dwEventFlags)
            {
            case 0:

                if (mer.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
                {
                    TCHAR c[2] = { '\0' };
                    DWORD cnum;
                    ReadConsoleOutputCharacter(
                        GetStdHandle(STD_OUTPUT_HANDLE),
                        c,
                        1,
                        coord,
                        &cnum
                        );
                    if (cnum == 1) MessageBox(NULL, c, NULL, 0);
                }
                break;
            case MOUSE_MOVED:
                //printf("mouse moved\n");
                coord.X = mer.dwMousePosition.X;
                coord.Y = mer.dwMousePosition.Y;
                break;
            default:
                break;
            }
        }
        else if (irInBuf[i].EventType == KEY_EVENT)
        {
            KEY_EVENT_RECORD ker = irInBuf[i].Event.KeyEvent;
            if (ker.bKeyDown)
            {
                DWORD written;
                WriteConsole(
                    GetStdHandle(STD_OUTPUT_HANDLE),
                    &ker.uChar,
                    1,
                    &written,
                    NULL
                    );
            }
        }
    }
}