Would you like to react to this message? Create an account in a few clicks or log in to continue.

Adding Color Text to C++ Console Application

2 posters

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

Go down

Adding Color Text to C++ Console Application Empty Adding Color Text to C++ Console Application

Post  David B 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.:

Adding Color Text to C++ Console Application Colore12
(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;
}
David B
David B
Administrator
Administrator

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

https://programmingforums.forumotion.com/

Back to top Go down

Adding Color Text to C++ Console Application Empty Re: Adding Color Text to C++ Console Application

Post  legolizard 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)
legolizard
legolizard

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

Back to top Go down

Adding Color Text to C++ Console Application Empty Re: Adding Color Text to C++ Console Application

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

Thank you lego.
David B
David B
Administrator
Administrator

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

https://programmingforums.forumotion.com/

Back to top Go down

Adding Color Text to C++ Console Application Empty Re: Adding Color Text to C++ Console Application

Post  legolizard Thu Jan 05, 2012 7:53 pm

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

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

Back to top Go down

Adding Color Text to C++ Console Application Empty Re: Adding Color Text to C++ Console Application

Post  David B 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.
David B
David B
Administrator
Administrator

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

https://programmingforums.forumotion.com/

Back to top Go down

Adding Color Text to C++ Console Application Empty Re: Adding Color Text to C++ Console Application

Post  David B 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.:

Adding Color Text to C++ Console Application Color_10

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

Adding Color Text to C++ Console Application Full_s10

Thanks again lego!
David B
David B
Administrator
Administrator

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

https://programmingforums.forumotion.com/

Back to top Go down

Adding Color Text to C++ Console Application Empty Re: Adding Color Text to C++ Console Application

Post  legolizard 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.
legolizard
legolizard

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

Back to top Go down

Adding Color Text to C++ Console Application Empty Re: Adding Color Text to C++ Console Application

Post  David B 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.
David B
David B
Administrator
Administrator

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

https://programmingforums.forumotion.com/

Back to top Go down

Adding Color Text to C++ Console Application Empty Re: Adding Color Text to C++ Console Application

Post  legolizard Sat Feb 11, 2012 9:01 pm

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

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

Back to top Go down

Adding Color Text to C++ Console Application Empty Re: Adding Color Text to C++ Console Application

Post  David B 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.
David B
David B
Administrator
Administrator

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

https://programmingforums.forumotion.com/

Back to top Go down

Adding Color Text to C++ Console Application Empty Re: Adding Color Text to C++ Console Application

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

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

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