Go Back   Forums > Community Chatterbox > Tech Corner > Programming
Memberlist Forum Rules Today's Posts
Search Forums:
Click here to use Advanced Search

Reply
 
Thread Tools Display Modes
Old 20-03-2010, 01:11 PM   #1
The Fifth Horseman
FUTURE SCIENCE BASTARD
 
The Fifth Horseman's Avatar


 
Join Date: Oct 2004
Location: Opole, Poland
Posts: 14,276
Default C++ text color include for Windows enviroment

I was messing with some things yesterday and got somewhat annoyed at the fact that while there is a way to change text and background color in a Windows console enviroment (for specific parts of the text rather than the whole thing at once), it's slightly unintuitive for the end user.

So, I cooked up a custom include with a few functions and definitions that make the color-changing a breeze:
Source code

(Attached as textcolor.h )
Note that negative argument values are used to reset the setting to default.
Attached Files
File Type: rar textcolor.rar (459 Bytes, 1 views)
__________________

"God. Can't you people see I'm trying to commit a crime against science and nature here?"
-- Reed Richards
The Fifth Horseman is offline                         Send a private message to The Fifth Horseman
Reply With Quote
Old 14-04-2010, 06:53 PM   #2
Peter
Forum hobbit

 
Join Date: May 2009
Location: Abo Tanga, Afghanistan
Posts: 28
Default

Some incomplete attempt to make it into a iomanipulator, didn't try if it compiles on Windows as I wrote it on Linux. Use it only with std::cout!
Code:
#include <iosfwd>
#include <windows.h>
class colors {
private:
        int val;
        friend std::ostream& operator<< (std::ostream& stream, colors const input) {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), input.val);
                return stream;
        }
public:
        colors(int a) {
                val = a < 0 ? 7 : (a % 256);
        }
        colors (int a, int b) {
                a = a < 0 ? 0 : (a % 16);
                b = b < 0 ? 7 : (b % 16);
                val = a * 16 + b;
        }
        enum { BLACK,BLUE,GREEN,CYAN,RED };
};
and a tiny example that uses it.
Put directly below the above "header"
Code:
#include <iostream>
int main () {
        std::cout << "test " << colors(257)<< "test2" << colors(colors::BLUE,10) << "test3" << std::endl;
        return 0;
}
You can implement manipulators in even shorter ways, but this one is pretty clean.

Last edited by Peter; 14-04-2010 at 07:08 PM.
Peter is offline                         Send a private message to Peter
Reply With Quote
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
DOSBox, The Random Color Glitch The_Lemming Troubleshooting 14 03-07-2012 08:33 PM
champions of krynn no sound, nu-rave color lord galen Troubleshooting 6 12-06-2009 02:16 PM
Red Baron (256-color VGA version) Miles On site 8 31-07-2008 12:59 PM
Color Lines troop18546 Approved Requests 3 13-09-2007 08:49 PM
Color Problem With Old Games Guest Tech Corner 7 29-08-2006 06:44 PM


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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump
 


The current time is 05:30 AM (GMT)

 
Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.