Solved

how to change text colour of output in c++?

Posted on 2003-11-05
5
515 Views
Last Modified: 2010-04-01
hello
      I have a problem while changing the colour of font in output
by using cout <<,
        please solve my problem and tell me how can i change the text colour n c++?
0
Comment
Question by:jadoogar
  • 2
5 Comments
 

Accepted Solution

by:
Larn0s earned 20 total points
ID: 9693582
#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

void DrawColorString(string szText, int X, int Y, WORD color);

int main(void) {

      DrawColorString("Red",  0, 0, FOREGROUND_RED);
      DrawColorString("White",5, 2, BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
      DrawColorString("Colored Text is Great!", 10, 4, FOREGROUND_RED | BACKGROUND_GREEN);

      return 0;
}

void DrawColorString(string szText, int X, int Y, WORD color) {

      HANDLE OutputH;
      COORD position = {X, Y};

      OutputH = GetStdHandle(STD_OUTPUT_HANDLE);
      SetConsoleTextAttribute(OutputH, color);
      SetConsoleCursorPosition(OutputH, position);

      cout << szText;
}
0
 
LVL 1

Expert Comment

by:Paladin_VB
ID: 9804542
.
0
 
LVL 1

Expert Comment

by:Paladin_VB
ID: 9804543
.
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10242566
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: Larn0s {http:#9693582}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now