Home > Windows API > gotoxy in a console application in windows

gotoxy in a console application in windows

gotoxy! what’s that?

gotoxy was one of my favorite functions in turbo c++, it moves the input cursor in a console application/MS-DOS application from one location to another.

So is there an equivalent function in a windows console application which does the same? Yes, it’s called SetConsoleCursorPosition.

Sample code

As always with me here is a function which does this!

BOOL gotoxy( const WORD x, const WORD y )
{
   COORD xy;
   xy.X = x;
   xy.Y = y;
   return SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), xy );
}
int main()
{
   if( !gotoxy( 10, 10 ))
   {
      std::cout < < "gotoxy failed :(, seems like coords are too big\n";       return 1;    }    return 0; }[/sourcecode] Result of calling "gotoxy", see how the pause message has shifted down and to the right.

Screenshot for gotoxy

Note that our gotoxy can fail if x or y is too big. A post from me will follow soon based on this function. It’s going to be about displaying a spining stick at the end of a progress message. You might have seen it if you have installed Linux.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s