Solved

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

Posted on 2003-11-05
5
512 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
Comment Utility
#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
Comment Utility
.
0
 
LVL 1

Expert Comment

by:Paladin_VB
Comment Utility
.
0
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 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

17 Experts available now in Live!

Get 1:1 Help Now