Move text cursor to particular screen coordinate?

后端 未结 7 1820
余生分开走
余生分开走 2020-12-31 19:01

How can I set the cursor at the desired location on the console in C or C++?

I remember a function called gotoxy(x,y), but I think its deprecated. Is th

7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-31 19:33

    I use a really simple method. You don't overly need to know what a HANDLE is unless you're really diving into console applications, a COORD object is in the windows.h standard library and has two member data intergers X and Y. 0,0 is the top left corner and Y increases to go down the screen. You can use this command and just continue to use std::cout<< to print whatever you need.

    #include 
    
    int main(void){
    //initialize objects for cursor manipulation
    HANDLE hStdout;
    COORD destCoord;
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    
    //position cursor at start of window
    destCoord.X = 0;
    destCoord.Y = 0;
    SetConsoleCursorPosition(hStdout, destCoord);
    }
    

提交回复
热议问题