1

I currently have the basic function to change the colour

void setColour(short colour){
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon, colour);
}
setColour(10);
//Previous text to be highlighted?
setColour(7); //Sets colour to default console colour

Using this is for printing new text in different colour I understand, but I'm currently creating a game that requires text to be highlighted that's already been printed. I know this can be done, but I don't know how..

Alex
  • 17
  • 4
  • 2
    I don't think you can change the color of the already written text. You can probably remove the old text and write the new one over it. – gmoshkin Apr 09 '17 at 18:43
  • Have a hunt through all the console functions for Windows. In the DOS days it was possible but we would write colour bytes directly to memory. Windows console is sorta emulating those old screen modes so you may be able to bake colours in. But the colour was interleaved with text anyway so it might be easiest to just reposition the cursor and rewrite the text, and MS may not have exposed a lower level of functionality. – paddy Apr 09 '17 at 18:51

2 Answers2

0

One way is to delete the current text and reprint the same text, anyhow systems today are lighting fast so you wont make out difference.

To do this we use \r

cout << "something" << '\r' << flush.
  • The '\r' means "carriage return", It will go to beginning of line.

  • And the 'flush' means "make sure what I've just printed reaches the output now.

After this reprint the same text with color of your choice.

NOTE: This happens so fast you will feel as if the text changed the colour.

Arvindsinc2
  • 716
  • 7
  • 18
0

As other people have said, you will need to re-print the same text in a different color to "change" the color.

However considering you are working with the Win32 console I think it would be more recommended to rely on platform dependant code -> Setting the Cursor Position in a Win32 Console Application

Moving the cursor around gives you more direct control of what you want to achieve (re-/over-writing already printed text in the console).

The other answer mentions printing '\r' to the console which does work, but only for the current line where as moving the cursor around yourself gives you the freedom to "change" any character(s) in the entire buffer of the console.

On the other hand as you have already mentioned there is also system("cls") which one might argue is a more "platform-independent" way of doing it, but its very slow (and kind of hackish) compared to a direct control so I would strongly recommend not to use it. (if you are wondering why, see system("pause"); - Why is it wrong?, granted its not the same command but the same principles apply)

Community
  • 1
  • 1
user2047610
  • 136
  • 1
  • 9
  • Thanks for the input, since this post I've tried to use "system("cls")" and has worked on highlighting the areas I want. I didn't consider this before because I thought it would be too slow/ sluggish and noticeable, but it's quite fast. – Alex Apr 09 '17 at 20:41
  • I think its slow enough to be noticable, especially when you are intending to use it for a game where updates in the buffer are fairly frequently required. But feel free to do it the way you think its best, its your choice after all :) – user2047610 Apr 09 '17 at 20:49