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

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




printf color

 
Reply to this topicStart new topic

printf color

Jingle
21 Nov, 2007 - 04:26 PM
Post #1

D.I.C Regular
***

Joined: 20 Oct, 2007
Posts: 250


My Contributions
i am trying to use printf and fprintf in color i have heard it is posible without going GUI and mesing with windows but i am clules where to start

This post has been edited by Jingle: 21 Nov, 2007 - 04:32 PM
User is offlineProfile CardPM
+Quote Post

htsh_shrm
RE: Printf Color
21 Nov, 2007 - 05:09 PM
Post #2

New D.I.C Head
*

Joined: 18 Oct, 2007
Posts: 25



Thanked: 1 times
My Contributions
you can use the function textcolor(COLOR NAME IN UPPERCASE);
eg: textcolor(RED);
or
textcolor(BLUE);
and many more;
you can also set the background color of the text being displayed
use textbackground(COLOR); for it
eg: textbackground(RED);
or
textbackground(LIGHTCYAN);

for more available colours scan the help index in the language.....................
User is offlineProfile CardPM
+Quote Post

Jingle
RE: Printf Color
21 Nov, 2007 - 05:41 PM
Post #3

D.I.C Regular
***

Joined: 20 Oct, 2007
Posts: 250


My Contributions
i dont think i know how to use that i have tried but it dose nothing no errors or results.
here is a section of my code im trying to change color
CODE

void faceBH(void)
{
printf("\n      !!!   \n");
printf("   . ~   ~ .  \n");
printf("  .  O   O  . \n");
printf("  .    ^    . \n");
printf("   . '~~' .   \n");
printf("      ..      \n");
}

i have made a bunch of these and have had lots of fun
i just think it would be even better to have them in color.
here is my atempt
CODE

void faceBH(void)
{
void textcolor(RED);  //if i dont ad the void i get an error
printf("\n      !!!   \n");  //it says it asumes it is an int
printf("   . ~   ~ .  \n");
printf("  .  O   O  . \n");
printf("  .    ^    . \n");
printf("   . '~~' .   \n");
printf("      ..      \n");
}

is it not in stdio.h ?
and dose it work on a mac (xcode)

This post has been edited by Jingle: 21 Nov, 2007 - 05:58 PM
User is offlineProfile CardPM
+Quote Post

htsh_shrm
RE: Printf Color
21 Nov, 2007 - 09:48 PM
Post #4

New D.I.C Head
*

Joined: 18 Oct, 2007
Posts: 25



Thanked: 1 times
My Contributions
use it in the function main()
as
int main()
{
textcolor(BLUE);
-
-
-
-
}
but i dont know wether it works on mac or not but i think that it should work
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Printf Color
21 Nov, 2007 - 11:23 PM
Post #5

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
Unfortunately color is not really standard. This gets to be very platform dependent. you can try this little program and see where it gets you:
CODE
#include <conio.h>
int main()
{
    textcolor(BLUE);
    cprintf("I'm blue.");
    return 0;
}
This is non-standard code and will not work on all compilers/platforms. However, keep in mind that your program will not be portable.

On Unix there is the curses library (#include <curses.h>) but this library is highly platform dependant (I remember that HP-UX was different from solaris, but this was "way back in the day").

For windows there are a number of functions to control the console in the windows api.
User is online!Profile CardPM
+Quote Post

Jingle
RE: Printf Color
21 Nov, 2007 - 11:31 PM
Post #6

D.I.C Regular
***

Joined: 20 Oct, 2007
Posts: 250


My Contributions
thanks i will se if i can get it working
but right now im going to bed its nearly 1:00 am
good night

User is offlineProfile CardPM
+Quote Post

htsh_shrm
RE: Printf Color
22 Nov, 2007 - 06:44 AM
Post #7

New D.I.C Head
*

Joined: 18 Oct, 2007
Posts: 25



Thanked: 1 times
My Contributions
see this was the easiest option thats y i told u this.
if this doesnt works then u will have to initiate the graphics mode by using initgraph() function. use the code as follows:
CODE


int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");   /*this function wil initiate the graphics mode*/
setcolor(BLUE);
setbkcolor(RED); /*this function will set the background color to red*/
outtextxy(20,20,"this will work");   /*it wil display the text starting from the (20,20) coordinate*/
closegraph();  /*always use this function at the end of the coding as it will close the graphics mode*/
}


but u cant use printf to display text in graphics mode
you will have to use outtextxy(x,y,"text");
where x ,y are the coordinates of the pixel from where the the text is to be displayed.
for more graphics functions u can scroll through the help index in the language.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 10:12PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

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