Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,263 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,497 people online right now. Registration is fast and FREE... Join Now!




Text and background color in C++

2 Pages V  1 2 >  
Reply to this topicStart new topic

Text and background color in C++

Rating  5
spooky
20 Oct, 2006 - 05:36 AM
Post #1

New D.I.C Head
*

Joined: 20 Oct, 2006
Posts: 10


My Contributions
hi guyzi'd like to know how to set back and text color in C++ Im creating a prog in DOS
no idea about windows programming wink2.gif

plz help me im searching all over the web and i can t find anythink!
oh i also what to know how i can make my prog opens in the whole screen

p.s sorry for my english... music.gif cool.gif
User is offlineProfile CardPM
+Quote Post

BitByte
RE: Text And Background Color In C++
20 Oct, 2006 - 07:22 AM
Post #2

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 2 times
My Contributions
Here's some code that might get you started. Did it a long time ago. I don't know how to set the background though rolleyes.gif

CODE
// February 2006 using excellent code::blocks IDE with mingw
// C++ Code for changing colors of text in the console window
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{

    HANDLE hConsole;
    hConsole = GetStdHandle (STD_OUTPUT_HANDLE);

// Use the three primary colors for mixing any other color.
// Use FOREGROUND_INTENSITY for brighter colors.
    SetConsoleTextAttribute
    (hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
            cout << "Bright red text\n";

    SetConsoleTextAttribute
    (hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
            cout << "Bright blue text\n";

    SetConsoleTextAttribute
    (hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE );
            cout << "Back to normal white text\n";

// Wait for user pressing key before exiting
// Gives them a chance to see the output
            cout << "\n\nPress any key to exit program.....";
            cin.get();

        return 0;
}

User is offlineProfile CardPM
+Quote Post

spooky
RE: Text And Background Color In C++
20 Oct, 2006 - 01:59 PM
Post #3

New D.I.C Head
*

Joined: 20 Oct, 2006
Posts: 10


My Contributions
well can i use other colors exept red or blue?
i tried yello and gray but nothink happent...

does anyone know how to chang back color?
User is offlineProfile CardPM
+Quote Post

BitByte
RE: Text And Background Color In C++
20 Oct, 2006 - 03:46 PM
Post #4

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 2 times
My Contributions
You can mix the colours just like you do when your painting. Red and blue makes purple etc....

FOREGROUND_RED | FOREGROUND_BLUE

Just mess about with the colours. About the background you just use BACKGROUND_RED or whatever colour you like. Just found that out. Should of figured it out really biggrin.gif
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Text And Background Color In C++
21 Oct, 2006 - 03:06 AM
Post #5

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
Woah...you needn't complicate your code for a DOS App.
Simply include conio.h and use these functions:
  • textcolor()
  • textbackground()
  • textattr()
Care must be taken to use cprintf() instead of printf() as printf() does not support formatted output.
Here's an example:
CODE

#include <conio.h>

int main(void)
{
   int i, j;

   clrscr();
   for (i=0; i<9; i++)
   {
       for (j=0; j<80; j++)
          cprintf("C");
       cprintf("\r\n");
       textcolor(i+1);
       textbackground(i);
   }

   return 0;
}

User is offlineProfile CardPM
+Quote Post

smg5k
RE: Text And Background Color In C++
21 Jan, 2008 - 09:50 AM
Post #6

New D.I.C Head
*

Joined: 26 Oct, 2007
Posts: 4


My Contributions
Is there any way of using cout instead of cprintf??
and is there something that colors all the background
not only the text area...??

This post has been edited by smg5k: 21 Jan, 2008 - 09:54 AM
User is offlineProfile CardPM
+Quote Post

Jingle
RE: Text And Background Color In C++
21 Jan, 2008 - 12:00 PM
Post #7

D.I.C Head
**

Joined: 20 Oct, 2007
Posts: 249


My Contributions
make sure to note that none of these will work on a mac or linix for that matter.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Text And Background Color In C++
21 Jan, 2008 - 12:06 PM
Post #8

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,515



Thanked: 67 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(Jingle @ 21 Jan, 2008 - 01:00 PM) *

make sure to note that none of these will work on a mac or linix for that matter.

Well, he did say that this was for DOS. For Linux colors, I'm sure you would want to use escape characters or the ncurses library (if you want portability).

CODE

#include <stdio.h>

int main(void) {
  printf("\033[22;31mHello world!");

  return 0;
}


QUOTE

\033[22;30m - black
\033[22;31m - red
\033[22;32m - green
\033[22;33m - brown
\033[22;34m - blue
\033[22;35m - magenta
\033[22;36m - cyan
\033[22;37m - gray
\033[01;30m - dark gray
\033[01;31m - light red
\033[01;32m - light green
\033[01;33m - yellow
\033[01;34m - light blue
\033[01;35m - light magenta
\033[01;36m - light cyan
\033[01;37m - white


http://www.vt100.net
User is online!Profile CardPM
+Quote Post

smg5k
RE: Text And Background Color In C++
21 Jan, 2008 - 12:37 PM
Post #9

New D.I.C Head
*

Joined: 26 Oct, 2007
Posts: 4


My Contributions
well it's not for dos... i want to use it in windows...
and don't know how...
User is offlineProfile CardPM
+Quote Post

Jingle
RE: Text And Background Color In C++
21 Jan, 2008 - 12:41 PM
Post #10

D.I.C Head
**

Joined: 20 Oct, 2007
Posts: 249


My Contributions
QUOTE(smg5k @ 21 Jan, 2008 - 01:37 PM) *

well it's not for dos... i want to use it in windows...
and don't know how...

the DOS prompt is part of windows



User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Text And Background Color In C++
21 Jan, 2008 - 12:42 PM
Post #11

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,515



Thanked: 67 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(spooky @ 20 Oct, 2006 - 06:36 AM) *

hi guyzi'd like to know how to set back and text color in C++ Im creating a prog in DOS

This is what I was referring too.
User is online!Profile CardPM
+Quote Post

smg5k
RE: Text And Background Color In C++
21 Jan, 2008 - 12:46 PM
Post #12

New D.I.C Head
*

Joined: 26 Oct, 2007
Posts: 4


My Contributions
sorry.. but it doesn't work.... it just shows me all the text including the color code
CODE

#include <stdio.h>

#include <windows.h>
  
int main(void) {
  
  printf("\033[22;33mHello world!");
  
  system("pause");
  
  return 0;
}

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/4/08 07:16PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month