我正在使用Visual Studio 2010,并且尝试在用户按键盘上的右数组键时移动光标:

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

void gotoxy(int x, int y)
{
  static HANDLE h = NULL;
  if(!h)
    h = GetStdHandle(STD_OUTPUT_HANDLE);
  COORD c = { x, y };
  SetConsoleCursorPosition(h,c);
}

int main()
{
    int Keys;
    int poz_x = 1;
    int poz_y = 1;
    gotoxy(poz_x,poz_y);

    while(true)
    {
        fflush(stdin);
        Keys = getch();
        if (Keys == 77)
                gotoxy(poz_x+1,poz_y);
    }

    cin.get();
    return 0;
}

它正在工作,但只有一次-第二,第三等按不起作用。

最佳答案

您永远不会更改poz_x,因此您总是会最终调用

gotoxy(2,1);

在循环。

关于c++ - C++在控制台中移动光标,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14404819/

10-12 12:37
查看更多