Solved

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

Posted on 2003-11-05
5
523 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Compile GLUT with Visual Studio 2015 1 206
C++ question 3 74
c++, dynamic object by json 1 60
Autosar OS Multicore Share Resources confusion ? 2 108
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

733 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