C++在控制台中移动光标 [英] C++ Move cursor in console

查看:0
本文介绍了C++在控制台中移动光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是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++在控制台中移动光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆