| View previous topic :: View next topic |
| Author |
Message |
clanner Master Cheater
Reputation: 0
Joined: 26 Jul 2006 Posts: 290
|
Posted: Mon Oct 20, 2008 4:53 pm Post subject: [C++] Help with coloring text |
|
|
Alright this is my code:
| Code: | #include <iostream>
#include <windows.h>
#include <string>
#include <stdlib.h>
using namespace std;
//Voids for colors
void blue()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
}
void red()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
}
void green()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
}
void cyan()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE | FOREGROUND_GREEN);
}
void yellow()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED);
}
void white()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
}
//end of voids for colors
int main()
{
blue();
cout << "This is blue!" << endl;
cout << "*****************";
white();
cout << "This is white!";
blue();
cout << "blue again!" << endl;
cout << "also blue!" << endl;
cin.sync();
cin.ignore();
return 0;
} |
What I want is this, the color part | Code: | | SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN) |
Is there a way instead of writing all that to make it be just like this? The reason for asking is because making all those voids make it look "messy".
| Code: | blue = GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN
SetConsoleTextAttribute(Blue) |
Thank you.
PS (I'm not sure if you will understand what I'm asking.)
|
|
| Back to top |
|
 |
Spawnfestis GO Moderator
Reputation: 0
Joined: 02 Nov 2007 Posts: 1746 Location: Pakistan
|
Posted: Mon Oct 20, 2008 5:01 pm Post subject: |
|
|
| Code: | private void setTextColor(string color)
{
Switch(color) {
case "blue":
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
break;
case "red":
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
break;
case "green":
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
break;
case "cyan":
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE | FOREGROUND_GREEN);
break;
case "yellow":
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED);
break;
case "white":
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
break;
default:
color == "white";
break;
} |
To assign a text color, simply write setTextColor("colorhere");
_________________
CLICK TO HAX MAPLESTORAY ^ !!!! |
|
| Back to top |
|
 |
GMZorita Grandmaster Cheater Supreme
Reputation: 0
Joined: 21 Mar 2007 Posts: 1361
|
Posted: Mon Oct 20, 2008 5:04 pm Post subject: |
|
|
| Code: | | #define blue GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE |
_________________
Gone |
|
| Back to top |
|
 |
Spawnfestis GO Moderator
Reputation: 0
Joined: 02 Nov 2007 Posts: 1746 Location: Pakistan
|
Posted: Mon Oct 20, 2008 5:06 pm Post subject: |
|
|
| GMZorita wrote: | | Code: | | #define blue GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE |
|
Ahh.
* I should start with C++ soon.
_________________
CLICK TO HAX MAPLESTORAY ^ !!!! |
|
| Back to top |
|
 |
GMZorita Grandmaster Cheater Supreme
Reputation: 0
Joined: 21 Mar 2007 Posts: 1361
|
Posted: Mon Oct 20, 2008 5:46 pm Post subject: |
|
|
If your going to use
| Code: |
#define blue GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE
SetConsoleTextAttribute(blue);
|
I suggest you to just use
| Code: |
#define blue() SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
|
Since your going to define to every colour anyway.
_________________
Gone |
|
| Back to top |
|
 |
Heartless I post too much
Reputation: 0
Joined: 03 Dec 2006 Posts: 2436
|
Posted: Mon Oct 20, 2008 6:06 pm Post subject: |
|
|
| Code: | void ChangeColor(int Color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), Color);
} |
white = 15
red = 10 or 12
green = the other one that is not red
That is all I remember off the top of my head.
_________________
What dosen't kill you, usually does the second time. |
|
| Back to top |
|
 |
clanner Master Cheater
Reputation: 0
Joined: 26 Jul 2006 Posts: 290
|
Posted: Mon Oct 20, 2008 7:32 pm Post subject: |
|
|
| GMZorita wrote: | If your going to use
| Code: |
#define blue GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE
SetConsoleTextAttribute(blue);
|
I suggest you to just use
| Code: |
#define blue() SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
|
Since your going to define to every colour anyway. |
| Code: |
#define blue() SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
|
Just curious but do I need to add the () after blue or can I just leave it as
|
|
| Back to top |
|
 |
smartz993 I post too much
Reputation: 2
Joined: 20 Jun 2006 Posts: 2013 Location: USA
|
Posted: Mon Oct 20, 2008 8:03 pm Post subject: |
|
|
| clanner wrote: | | GMZorita wrote: | If your going to use
| Code: |
#define blue GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE
SetConsoleTextAttribute(blue);
|
I suggest you to just use
| Code: |
#define blue() SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
|
Since your going to define to every colour anyway. |
| Code: |
#define blue() SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
|
Just curious but do I need to add the () after blue or can I just leave it as
|
You need the parentheses, you are defining a function, all functions have the parentheses whether empty or with parameters.
|
|
| Back to top |
|
 |
clanner Master Cheater
Reputation: 0
Joined: 26 Jul 2006 Posts: 290
|
Posted: Mon Oct 20, 2008 8:11 pm Post subject: |
|
|
| Okay thank you!
|
|
| Back to top |
|
 |
nog_lorp Grandmaster Cheater
Reputation: 0
Joined: 26 Feb 2006 Posts: 743
|
Posted: Mon Oct 20, 2008 9:11 pm Post subject: |
|
|
What? He doesn't need the parenthesis. He is not defining a function, if he were to use parenthesis it would be a macro. He has no need for that since he is not passing a variable to the macro.
_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish |
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Tue Oct 21, 2008 1:09 am Post subject: |
|
|
| Code: | | #define WHITE FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_RED|FOREGROUND_GREEN |
No need to call GetStdHandle EVERY time.
|
|
| Back to top |
|
 |
clanner Master Cheater
Reputation: 0
Joined: 26 Jul 2006 Posts: 290
|
Posted: Tue Oct 21, 2008 8:07 am Post subject: |
|
|
| What exactly does GetStdHandle do? I just copied the code from somewhere and understood "most" of it except for that.
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
|
| Back to top |
|
 |
|