C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 7th, 2006, 03:48 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 346 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 14 h 14 m 58 sec
Reputation Power: 3
Console Text Color

Hi all,
After hours of searching the internet for a solution on how to change the color of the console text color i found this

#include <stdio.h>
#include <windows.h> // WinApi header

int main()
{
HANDLE hConsole;
int k;

hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

// you can loop k higher to see more color choices
for(k = 1; k < 255; k++)
{
SetConsoleTextAttribute(hConsole, k);
printf("%3d %s\n", k, "I want to be nice today!");
}

getchar(); // wait
return 0;

After modifying it because it would not work on Borland 5.5, i came up with this :

#include <iostream>
#include <windows> // WinApi header

using namespace std;

int main()
{
HANDLE hConsole;
int k = 10;

hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, k);
cout << "Color in console!\n", k; //Green Text Color

cin.get();
return 0;
}

Now what i want to know is how can i have the text change to any color i want when i want? At the moment i can only use one color at a time. If i change the color to another color just before i want to use it, the compiler says it's an error. What would i have to do to make this work?

I'm using windows XP, and Borland 5.5 commandline compiler. I hope i have been specific enough.

Many Thanks in advance. Paul820

Reply With Quote
  #2  
Old February 8th, 2006, 01:22 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 755 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 14 h 48 m 22 sec
Reputation Power: 4
I'm not sure how things work in windows but for linux you can use ANSI colour codes. Second hit on google seems to explain nicely:
http://www.tldp.org/linuxfocus/Engl...rticle335.shtml

There is a sample with C code. Let me know if this also works for Windows.

Reply With Quote
  #3  
Old February 8th, 2006, 11:37 AM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 346 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 14 h 14 m 58 sec
Reputation Power: 3
Tried it

Quote:
Originally Posted by Icon
I'm not sure how things work in windows but for linux you can use ANSI colour codes. Second hit on google seems to explain nicely:
http://www.tldp.org/linuxfocus/Engl...rticle335.shtml

There is a sample with C code. Let me know if this also works for Windows.


I went to the site you said and tried the bit of C code that was there. I had to download pelles c compiler to see if it would work on it. It never even worked on a c compiler. I modified it a bit to try and get it to work on Borland, it worked, erm sort of, it just printed out (\033[0;40;32m) <--- That bit, nothing else. Maybe i will have to do a bit more searching. I didn't think getting the colors to change would be so complicated. Oh by the way, i used this code from that site :

#include <stdio.h>
int main(void){
const char *const green = "\033[0;40;32m";
const char *const normal = "\033[0m";
printf("%sHello World%s\n", green, normal);
return 0;
}

Thanks for answering my question. I'll keep plodding on. Many thanks

Paul820

Reply With Quote
  #4  
Old February 8th, 2006, 02:56 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 755 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 14 h 48 m 22 sec
Reputation Power: 4
Ok I had time to check it out myself.. It does not work, sorry.. I compiled your code on my linux box and it worked. Then I cross-compiled it (i have no c compilers on my windows box and cross-compiling windows programs from linux just rocks!) but it did not work on windows. Which is could have known in advance since those escape codes are some terminal-specific thing.. I'll look into some more if I can.

Reply With Quote
  #5  
Old February 8th, 2006, 03:00 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 755 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 14 h 48 m 22 sec
Reputation Power: 4
After some digging I see that the trick you are using is the way to do it (under C# there might be a console object but we are not using .not).. _plodding on_ as well..

Reply With Quote
  #6  
Old February 8th, 2006, 07:06 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 346 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 14 h 14 m 58 sec
Reputation Power: 3
I've done it!!!

After some digging I see that the trick you are using is the way to do it (under C# there might be a console object but we are not using .not).. _plodding on_ as well..[/QUOTE]

Well have loads of digging around, reading bits of code i finally got it to work so i can use any color when i want. Heres the code:

#include <iostream>
#include <windows>

using namespace std;

int main()
{

HANDLE hConsole;
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);

SetConsoleTextAttribute
(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
cout << "Red Text\n";

SetConsoleTextAttribute
(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
cout << "Blue Text\n";

cin.get();

return 0;
}

There are loads of people on google trying to find out how to do it but no answers. If anyone wants to use it go ahead. Thanks for pointing me in the right direction Icon, appreciate that, couldn't have done it without you. Now i can get on with doing some more programming (with color).

By the way, it's compiles with free Borland C++ 5.5, don't know about any others.

(UPDATED)
Just found out it isn't quite right yet, got ahead of myself there. It only lets you use three colors RED, BLUE and GREEN. Looks like it's back to the drawing board again

paul820

Last edited by Paul820 : February 8th, 2006 at 07:26 PM. Reason: It isn't quite right afterall

Reply With Quote
  #7  
Old February 9th, 2006, 01:38 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 755 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 14 h 48 m 22 sec
Reputation Power: 4
Ok I'm at work now, so I don't have time to do anything myself (nor do I have a C(++) compiler here *gasp*). But I found this website which explains how to make character attributes:
http://msdn.microsoft.com/library/d...een_buffers.asp

The idea is to 'mix' the three primary colours and an intensity to obtain any colour you want (think of the colour palettes in photoshop or paint). If I have time tonight (doubt it, sorry) then I'll try to elaborate on it. Hope this helps you a little.

Note: So in fact you were already there, you just didn't know it, keep the drawing board

Last edited by Icon : February 9th, 2006 at 01:40 AM.

Reply With Quote
  #8  
Old February 9th, 2006, 01:56 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 755 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 14 h 48 m 22 sec
Reputation Power: 4
A big example (not just about colours though) can be found here:
http://msdn.microsoft.com/library/d...functions.as p

In your first example you are ranging k from 0 to 255. The character attribute parameter is of type Word which is unsigned 16 bit (2 bytes) which ranges from 0..65535 so ...

Reply With Quote
  #9  
Old February 9th, 2006, 09:10 AM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 346 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 14 h 14 m 58 sec
Reputation Power: 3
Mixing colors

Quote:
Originally Posted by Icon
A big example (not just about colours though) can be found here:
http://msdn.microsoft.com/library/d...functions.as p

In your first example you are ranging k from 0 to 255. The character attribute parameter is of type Word which is unsigned 16 bit (2 bytes) which ranges from 0..65535 so ...


Thanks Icon, i never thought of mixing the colors, i assumed i could just use the colors by name, like FOREGROUND_YELLOW. I saw another example similar that did this. Never entered my mind about mixing the colors though. Going to have a good read on microsofts website now to see what else i can find out. Thanks for the links.
So i did do it after all, sort of, with your help anyway, thanks.

Paul820

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Console Text Color


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT