Getting the location of the cursor in the console

Aug 16, 2011 at 4:24pm
I had been having a look on the console functions in Windows.h header in the documentation in the MSDN site.
I tried using the FillConsoleOutputAttribute() function and noticed that it doesn't set the color of the text after it.
Then I used the SetConsoleTextAttribute() function.

Now the thing I found was that if I changed the color of the program during runtime at various points, the color of the text preceding the current text would change its color as to the current one as well.

I understood that it was because I was setting the X and Y coordinate to 0.

Now to the problem I had:
Is there a way to get the coordinates of the cursor at a given time so that they can be put into a COORD structure?
Aug 16, 2011 at 4:39pm
Aug 17, 2011 at 8:06am
Thanks for the link got it working.

Just I was having a doubt with two things:
1:
In the wherex Function:

1
2
3
4
5
6
7
8
9
10
11
int wherex()
{
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	COORD                      result;
	if (!GetConsoleScreenBufferInfo(
		GetStdHandle( STD_OUTPUT_HANDLE ),
		&csbi
		))
		return -1;
	return result.X;
}


How does result get the info of the current coordinates of the cursor?
I don't see any way where data may be transferred from csbi to result.


2:
Is gotoxy reading the coordinates in the form of characters?
Like by column 5 will mean the fifth character?

And thanks again for the help.
Aug 17, 2011 at 12:01pm
Ack! That code is missing something! (Sorry!)

The COORD position of the cursor is in csbi.dwCursorPosition. (I'm not sure why I've got that 'result' variable there instead...)

1
2
3
4
5
6
7
8
9
10
int wherex()
{
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	if (!GetConsoleScreenBufferInfo(
		GetStdHandle( STD_OUTPUT_HANDLE ),
		&csbi
		))
		return -1;
	return csbi.dwCursorPosition.X;
}

Sorry again for the goof up!
Aug 17, 2011 at 12:28pm
Thanks for clearing that up!
Topic archived. No new replies allowed.