Question

PS: I am using Code::blocks
no conio2.h available

I want to set the font color to black by windows API

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), XXXXXX);

What should I fill in XXXXXX?

Was it helpful?

Solution

From the documentation:

The foreground attributes specify the text color. The background attributes specify the color used to fill the cell's background. The other attributes are used with DBCS.

An application can combine the foreground and background constants to achieve different colors. For example, the following combination results in bright cyan text on a blue background.

FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_BLUE\

If no background constant is specified, the background is black, and if no foreground constant is specified, the text is black. For example, the following combination produces black text on a white background.

BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED

OTHER TIPS

The font color is represented by the combination of several possible attributes:

FOREGROUND_BLUE       Text color contains blue.  
FOREGROUND_GREEN      Text color contains green.  
FOREGROUND_RED        Text color contains red.  
FOREGROUND_INTENSITY  Text color is intensified.  

In your case, since you want the foreground color to be black, you have to pass no attributes at all:

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0);

As Ben Voigt points out in the comments below, this will result in black text on a black background. You might want to specify a combination of background attributes in order for the text to readable. For instance (black on white):

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
    BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top