DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Change text color using visual c++ (http://www.daniweb.com/forums/thread9921.html)

hail2dthief Aug 27th, 2004 2:16 am
Change text color using visual c++
 
I'm using visual c++ compiler and I want to change the text color in my c++ dos program. What choices do I have if i don't wanna use system function (example: system("color 0a"))?

FireNet Aug 27th, 2004 2:40 am
Re: Change text color using visual c++
 
conio.h

It has a lot funtions for console input/output.Take a look at it.

TextColor(RED);
TextBackground(BLUE);

The color values range from 0-15 with 0 being black and 15 being white

warning,i am not too sure of the funtion names,been a while since i used this, though i used it almost everywhere, man time flies

hail2dthief Aug 29th, 2004 11:34 pm
Re: Change text color using visual c++
 
Thanks, FireNet. But the trick doesn't work for microsoft visual c++ 6.0 compiler, is there any function that i can use to change text color using visual c++?

Brian The Mad May 4th, 2006 11:56 am
Re: Change text color using visual c++
 
Quote:

Originally Posted by hail2dthief
I'm using visual c++ compiler and I want to change the text color in my c++ dos program. What choices do I have if i don't wanna use system function (example: system("color 0a"))?

#define BLACK 0
#define BLUE 1
#define GREEN 2
#define CYAN 3
#define RED 4
#define MAGENTA 5
#define BROWN 6
#define LIGHTGREY 7
#define DARKGREY 8
#define LIGHTBLUE 9
#define LIGHTGREEN 10
#define LIGHTCYAN 11
#define LIGHTRED 12
#define LIGHTMAGENTA 13
#define YELLOW 14
#define WHITE 15
#define BLINK 128
HANDLE  screen;
int textcolor = LIGHTGREEN;
int backgroundcolor = BLACK;
screen = GetStdHandle(STD_OUTPUT_HANDLE);
void TextColor(int fontcolor,int backgroundcolor,HANDLE screen)
{
  int color_attribute;
  color_attribute = backgroundcolor;
  color_attribute = _rotl(color_attribute,4) | fontcolor;
  SetConsoleTextAttribute(screen,color_attribute);
}
 
TextColor(textcolor,backgroundcolor ,screen);
FillConsoleOutputAttribute(screen, _rotl(backgroundcolor,4) , 80 * 50,coord , &cWritten);


This works in Microsoft c++

MagikMan74 Sep 26th, 2007 2:06 pm
Re: Change text color using visual c++
 
Of course, if you're just wanting to change the color as you're typing it, then the above is a bit much. A simpler way would be to use this:
 
#include <windows.h>
#include <iostream.h>
 
int main()
{
  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0); //replace the 0 with a number for the color you want
 
  cout << "Your text here" << endl;
 
  return 0;
}

ShawnCplus Sep 26th, 2007 5:29 pm
Re: Change text color using visual c++
 
I used this one to save me some time
This is not Visual C++ dependant, I use this with DEv-C++ as well, you just need the windows header
enum Colors { blue=1, green, cyan, red, purple, yellow, grey, dgrey, hblue, hgreen, hred, hpurple, hyellow, hwhite };

void coutc(int color, char* output)
{
  HANDLE handle= GetStdHandle(STD_OUTPUT_HANDLE);
  SetConsoleTextAttribute( handle, color);
  cout<< output;
  SetConsoleTextAttribute( handle, color);
}

Then to output in color you would just do
coutc(red, "This is in red!");
coutc(purple, "This is purple!");

|\|asrin Jan 22nd, 2009 7:15 am
Re: Change background color using visual c++
 
HELLO
Can sombody tell me how can i chang the color of concol background????
Quote:

Originally Posted by MagikMan74 (Post 441221)
Of course, if you're just wanting to change the color as you're typing it, then the above is a bit much. A simpler way would be to use this:
 
#include <windows.h>
#include <iostream.h>
 
int main()
{
  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0); //replace the 0 with a number for the color you want
 
  cout << "Your text here" << endl;
 
  return 0;
}


mostermand Jan 22nd, 2009 12:50 pm
Re: Change text color using visual c++
 
Try this.

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_RED BACKGROUND_INTENSITY);

You can change RED to GREEN or BLUE and BACKGROUND to FOREGROUND.

If you want to mix colors you just separate them with a |

Comatose Jan 22nd, 2009 2:48 pm
Re: Change background color using visual c++
 
Quote:

Originally Posted by |\|asrin (Post 784810)
HELLO
Can sombody tell me how can i chang the color of concol background????

HELLO... can someone tell me how I can get people to quit posting on ancient old threads?

|\|asrin Jan 23rd, 2009 11:50 am
Re: Change text color using visual c++
 
Hi

Thank you for your reply mostermand but that code didnt work in
microsoft vitual c++(I dont know why??). after searching in the net for hours I found a
useful site about win32 consul application, I thought that it would be very useful for beginners .
http://www.adrianxw.dk/SoftwareSite/index.html

Bye

phr34k1sh Mar 14th, 2009 1:22 am
Simple way...
 
Alright... this is all i do... inside of main... right before i access cout... i simply just put on a line before this...

system("color 0a");

This is processing a batch command... and batch is quite simple... if you would like a definition of how this works, simply go to "cmd.exe" and type "help color" this will explain how to use this... you can change the background color of the console with this method... here is an example...

#include "header.h"

using namespace std;

int main ()
{
        system("title Hello World.");
        system("color 0a");
        cout<<"Hello world.";
        system("pause >nul");
        return 0;
}

header.h contains
#include <iostream>

so if you're using an older c++ compiler... replace
#include "header.h"
with
#include <iostream>

otherwise you know what to do... peace... hope i could help.


All times are GMT -4. The time now is 2:26 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC