Solved

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

Posted on 2003-11-05
5
522 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

856 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