Move text cursor to particular screen coordinate?

后端 未结 7 1818
余生分开走
余生分开走 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 <windows.h>
    
    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);
    }
    
    0 讨论(0)
  • 2020-12-31 19:36

    In case you are talking about ncurses library, the function you are after is move (row, column).

    0 讨论(0)
  • 2020-12-31 19:39

    I figured out this to set the cursor.

    #include <iostream>
    
    void setPos(std::ostream& _os, const std::streamsize& _x, const std::streamsize& _y)
    {
        char tmp = _os.fill();
    
        if(_y>0) {
                _os.fill('\n');
                _os.width(_y);
                _os << '\n';
        }
        if(_x>0) {
                _os.fill(' ');
                _os.width(_x);
                _os << ' ';
        }
        _os.flush();
        _os.fill(tmp);
    }
    
    int main(int argc, char **argv)
    {
        setPos(std::cout, 5, 5);
        std::cout << "foo" << std::endl;
        return 0;
    }
    

    To do more you'll need assumptions on the resolution or a lib like ncurses.

    0 讨论(0)
  • 2020-12-31 19:50

    Use SetConsoleCursorPosition.

    There are a bunch of other functions in the same part of the MSDN library. Some of them may be useful too.

    0 讨论(0)
  • 2020-12-31 19:51

    You can use this to set the cursor to specific coordinates on the screen, and then simply use cout<< or printf statement to print anything on the console:

    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    void set_cursor(int,int);
    
    int main()
    {
         int x=0 , y=0;
         set_cursor(x,y);
         cout<<"Mohammad Usman Sajid";
    
         return 0;
    }
    
    void set_cursor(int x = 0 , int y = 0)
    {
        HANDLE handle;
        COORD coordinates;
        handle = GetStdHandle(STD_OUTPUT_HANDLE);
        coordinates.X = x;
        coordinates.Y = y;
        SetConsoleCursorPosition ( handle , coordinates );
    }
    
    0 讨论(0)
  • 2020-12-31 19:53

    This was on stackoverflow...

    `#include <stdio.h>
    
    // ESC-H, ESC-J (I remember using this sequence on VTs)
    #define clear() printf("\033[H\033[J")
    
    //ESC-BRACK-column;row (same here, used on terminals on an early intranet)
    #define gotoxy(x,y) printf("\033[%d;%dH", (y), (x))
    
    int main(void)
    {
        clear();
        gotoxy(23, 12);
        printf("x");
        gotoxy(1, 24);
        return 0;
    }`
    
    0 讨论(0)
提交回复
热议问题