SetConsoleTextAttribute : Foreground only

Hi,

I would like to change ONLY the foreground color text of my console application, not the background text color nor the console background color. In Other words: I want to keep previous colors like they are except the foreground text color.

Presently i use the code below but the background under the text change too.

Thanks in advance for your help,

MoonDragon

1
2
3
4
5
6
7
8
9
10
11
12
#include <windows.h>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{

	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
	cout << "green?" << endl;
	cin.ignore();
	return 0;
}
Set the background colour as well (otherwise it will go to black)
For example:
Green writing on Red background (note: use the bitwise OR operator | )

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | BACKGROUND_RED);
Last edited on
Thanks.
Topic archived. No new replies allowed.