Adding Color Text to C++ Console Application

 :: C Languages :: C++ :: Help!

View previous topic View next topic Go down

Adding Color Text to C++ Console Application

Post  David B on Mon Jan 02, 2012 12:58 am

Hello!

I was wondering how one would go about adding colored text to a C console application. I know it can be done because I found this.

On that website, I found this image, which proves that it can be done.:


(Obviously, this screenshot was not taken by me.)

I tried to compile the following code, without modifying it in any way, but it didn't work. I checked for basic syntax errors, of which I found none. Not only that, but I did not know what any of the errors meant.

Code:
#include "Console.h"

namespace con = JadedHoboConsole;

int main()
{
    using std::cout;
    using std::endl;

    cout << "I like " << con::fg_green << "green" << con::fg_white << " eggs and ham." << endl;
}

_________________
Programming Language Community Forums Administrator,








___________________



___________________

avatar
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

View user profile http://programmingforums.forumotion.com/

Back to top Go down

Re: Adding Color Text to C++ Console Application

Post  legolizard on Mon Jan 02, 2012 1:36 pm

That code does not work because you don't have the Console.h header file. Now there are multiple ways to add color to a C++ console app and if you really want to use the above stuff then I suggest looking for the header file somewhere. xD

Here are some ways I know how to do so however at varying difficulty.(Stupid WinAPI:////).

All of the below require this header file : Windows.h and correspondingly this line of code :
Code:
#include <Windows.h>

Here is the first method...
Code:
system("COLOR );
This call takes one string literal that is comprised of 3 specific parts. One the COLOR command, two the background color number and 3 the text color number.
Here are the color numbers, now I don't use this function often so you may have to experiment a bit in case my memory is wrong. :-/(And I am too lazy to test it out myself. xDDD)

0 = Black
1 = Blue
2 = Green
3 = Light blue?
4 = Red
5 = Purple
6 = Yellow?
7 = White
8 = Gray
9 = Another blue I think....
Then I think it switches to letters...
A = Light Green
B = Light Aqua
C = Some sort of red...I think...
D = Light Purple
E = Light Yellow
F = I honestly forget. xD
This wil make the background black and the text red.
Code:
system("COLOR 04");
A more complex way that uses the WinAPI goes like this...
Code:
#include <Windows.h>
#include <iostream>

int main(){
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

for(int colorCode = 1; colorCode < 255; colorCode++){
SetConsoleTextAttribute(hConsole, colorCode);
std::cout <<"LAWLALWALWALAWLLWA\n";
}

std::cin.get();
return 0;
}

EDIT :

LAWL I found it on Code Project and I guess you did too. xD Good thing I had an account. c:

Here is the header file if you wish to have it and use your method. :-)
Code:
///@note I did not under no circumstances write this file, and all credit goes to the respective authors of the file.

http://tinyurl.com/86dyoqa

lulZ They use the really hard WinAPI method. LAWL I did not want to put that hear since I barely even get it but yeah.... XD


Last edited by legolizard on Mon Jan 02, 2012 5:35 pm; edited 2 times in total (Reason for editing : Replaced Download URL http://www.mediafire.com/?72jrt3a2kpx76kd)
avatar
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

View user profile

Back to top Go down

Re: Adding Color Text to C++ Console Application

Post  David B on Mon Jan 02, 2012 3:38 pm

Thank you lego.

_________________
Programming Language Community Forums Administrator,








___________________



___________________

avatar
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

View user profile http://programmingforums.forumotion.com/

Back to top Go down

Re: Adding Color Text to C++ Console Application

Post  legolizard on Thu Jan 05, 2012 7:53 pm

No problem mate. Which method did you end up using?
avatar
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

View user profile

Back to top Go down

Re: Adding Color Text to C++ Console Application

Post  David B on Thu Jan 05, 2012 10:03 pm

legolizard wrote:No problem mate. Which method did you end up using?
Right now, I have not used this thread for anything, but I will be in the near future.

_________________
Programming Language Community Forums Administrator,








___________________



___________________

avatar
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

View user profile http://programmingforums.forumotion.com/

Back to top Go down

Re: Adding Color Text to C++ Console Application

Post  David B on Sat Feb 11, 2012 7:20 pm

legolizard wrote:No problem mate. Which method did you end up using?
To answer your question about which method I ended up using lego, I ended up using your method because I could not get Console.h to work, so I compiled this...

legolizard wrote:
Code:
#include <Windows.h>
#include <iostream>

int main(){
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

for(int colorCode = 1; colorCode < 255; colorCode++){
SetConsoleTextAttribute(hConsole, colorCode);
std::cout <<"LAWLALWALWALAWLLWA\n";
}

std::cin.get();
return 0;
}
...and got this.:



I know that this image doesn't show the entire screen, so here is an image of the entire screen.



Thanks again lego!
avatar
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

View user profile http://programmingforums.forumotion.com/

Back to top Go down

Re: Adding Color Text to C++ Console Application

Post  legolizard on Sat Feb 11, 2012 7:57 pm

Yup anytime. But if you want me to show you how to use the header file I would be glad to. May also give me impetus to make another tutorial haha.
avatar
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

View user profile

Back to top Go down

Re: Adding Color Text to C++ Console Application

Post  David B on Sat Feb 11, 2012 8:37 pm

legolizard wrote:Yup anytime. But if you want me to show you how to use the header file I would be glad to. May also give me impetus to make another tutorial haha.
You should do something more basic before you make a tutorial on this.

_________________
Programming Language Community Forums Administrator,








___________________



___________________

avatar
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

View user profile http://programmingforums.forumotion.com/

Back to top Go down

Re: Adding Color Text to C++ Console Application

Post  legolizard on Sat Feb 11, 2012 9:01 pm

Ah okay, but would you still like to know how to use header files? 0o0
avatar
legolizard

Posts : 137
Join date : 2011-08-01
Location : On planet Char.

View user profile

Back to top Go down

Re: Adding Color Text to C++ Console Application

Post  David B on Sat Feb 11, 2012 9:32 pm

legolizard wrote:Ah okay, but would you still like to know how to use header files? 0o0
Yes, but not right now.

_________________
Programming Language Community Forums Administrator,








___________________



___________________

avatar
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

View user profile http://programmingforums.forumotion.com/

Back to top Go down

Re: Adding Color Text to C++ Console Application

Post  Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 :: C Languages :: C++ :: Help!

 
Permissions in this forum:
You cannot reply to topics in this forum