#include <iostream>
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
void setcolor( unsignedchar color )
{
SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), color );
}
int main()
{
setcolor( 0x07 ); // Initial color for program
std::cout << "Hello ";
setcolor( 0x0C );
std::cout << "I'm";
setcolor( 0x07 );
std::cout << " Bob.\n";
}
Be aware, this is a simple example. You should typically use GetConsoleScreenBufferInfo() to learn the current color, then initialize it to the color your application will use by default, and restore the original color before your application terminates.
thanks this is helpful! do you know by any chance how to change a words color only when the cursor is fixed on the words position ?
i use: void gotoxy(int x, int y)
{
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
COORD position = { x, y };
SetConsoleCursorPosition(hStdout, position);
}
(function to change the cursors position)
now if i have a word on (16,2) coordinates and the cursor is on the same word it should be colored red if the cursor doesn't point to that position it should be the default white color.
thanks everyone for help i have done what i needed without coloring by using cursor position and moving pointers on options but i learned a new thing from your comments!