Console app coloring.

Hello, I am creating app like Micro. Off. Excel in console ...
This is where I am currently : http://prntscr.com/9jtmts

So I wonder Is it possible to color specific location in console app just like I colored one of the cells But without recreating the whole table(in my case) ?

Thanks for any help ! :)
In windows console apps, you can use the function SetConsoleCursorPosition to set the current position of the cursor. This way, you can overwrite just 2 or 3 characters (one cell) instead of rewriting the entire table. You can use colors in the console using SetConsoleTextAttribute (seeing you already managed to color a cell, I think you already know this).

Links to MSDN on the functions:

SetConsoleCursorPosition: https://msdn.microsoft.com/nl-nl/library/windows/desktop/ms686025%28v=vs.85%29.aspx
SetConsoleTextAttribute: https://msdn.microsoft.com/en-us/library/windows/desktop/ms686047%28v=vs.85%29.aspx
Thank you very much Shadowwolf, I already knew how to use a SetConsoleCursorPosition function but I didn't know It can replace text if you type text at position where the text is already written. So here is my Solution if anyone doesn't know how to achieve this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void getCursorXY(int &x, int&y) {
	
		CONSOLE_SCREEN_BUFFER_INFO csbi;
	
		if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {	
			x = csbi.dwCursorPosition.X;
			y = csbi.dwCursorPosition.Y;		
		}
}
void gotoxy(int x, int y)
{
	COORD coord;
	coord.X = x;
	coord.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}
int CreateTable() {
//IN FOR LOOP I GET THIS POINTS :
getCursorXY(X1, Y1); //AT THE BEGINING OF TABLE I GET X1 Y1 Pos
getCursorXY(X2, Y2); //AT THE END OF TABLE I GET X2 Y2 Pos
//

//Coloring : 
	HANDLE  hConsole;
	hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        
      SetConsoleTextAttribute(hConsole, 68);
	gotoxy(X1, Y1+1); // JUST EXAMPLE
	cout << "         ";
	gotoxy(X1, Y2 + 1);
	SetConsoleTextAttribute(hConsole, 15);

return 0;
}

That's pretty much it :D Thanks a lot once more !
THIS IS THE RESULT : http://prntscr.com/9kb4gq
Last edited on
Topic archived. No new replies allowed.