Colors in Consol programming

This is a discussion on Colors in Consol programming within the C++ Programming forums, part of the General Programming Boards category; Hey, I just have a little question...I was woundering if there is a way to make consoleprograms with blue(or any ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    11

    Colors in Consol programming

    Hey, I just have a little question...I was woundering if there is a way to make consoleprograms with blue(or any other colors) letters/characters insted of white..do I have to do something with ANSI or ?
    btw: Im coding in MS VC++6.0 std.edition
    Thx

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    Here's a code snippet I took from MSDN and modified to get you going. If you want more info look in wincon.h it declares all the structures, attributes and APIs.

    Code:
    
    #include "stdafx.h"
    #include <windows.h>
    #include <iostream> 
    
    using namespace std; 
    
    HANDLE hStdout, hStdin; 
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
    
    int main() 
    {  
    
    
    	// Get handle to STDIN. 
    
    	hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
    	if ( hStdout == INVALID_HANDLE_VALUE ) 
    	{
    		cout << "GetStdHandle" << endl; 
    		return 1;
    	}
    
    	// Save the current text colors. 
    
    	if ( ! GetConsoleScreenBufferInfo( hStdout, &csbiInfo) ) 
    		cout << "GetConsoleScreenBufferInfo" << endl; 
    
    
    	// Set the text attr. to draw red text on black background. 
    
    	if ( ! SetConsoleTextAttribute( hStdout, FOREGROUND_GREEN | BACKGROUND_RED  ) ) 
    		cout << "SetConsoleTextAttribute" << endl; 
    
    	cout << "this should show new attribute " << endl;
    
    	cout << "press any key to reset console" << endl;
    
    	cin.get( ); 
    
    	if ( ! SetConsoleTextAttribute( hStdout, csbiInfo.wAttributes ) ) 
    		cout << "Reseting SetConsoleTextAttribute" << endl; 
    
    	cout << "this should show old attributes " << endl;
    
    	return 0; 
    }
    Last edited by Dang; 09-21-2001 at 11:14 PM.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    22
    This is easier than the previous...

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),FOREGROUND_BLUE);

    Or for a brighter blue...

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),FOREGROUND_BLUE | FOREGROUND_INTENSITY);

    Just add this line as the very first line in your main() function.
    <^>( * ; * )<^>

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    While it is easier it incurs the overhead of executing the GetStdHandle function, and with all function calls (except inline) that incurs the overhead of the prologue and epilogue every time you change a console color. In this case that probably wouldn't add up to much, but I think itís important to think about what's going on under the hood and attempt to work at efficiency.

    It also lacks error checking.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    11

    Thumbs up

    Yeah, i think i got it new =) Thx guys =)

    I was sitting and trying to use the ANSI.SYS file, it worked fine..but I had to make a .bat file to use it, and start it everytime im in DOS =(

    thx again

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    11

    Angry lol

    typos..
    *now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why I only get 8 colors out of ncurses?
    By Nazgulled in forum C Programming
    Replies: 3
    Last Post: 05-08-2007, 06:06 PM
  2. Relation between material colors and light colors.
    By indigo0086 in forum Game Programming
    Replies: 3
    Last Post: 04-18-2007, 03:20 PM
  3. colors in forefox and colors in IE
    By MisterSako in forum Tech Board
    Replies: 3
    Last Post: 05-15-2006, 01:59 PM
  4. different colors
    By Paveltc in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-19-2002, 10:21 AM
  5. Text Colors
    By GaPe in forum C Programming
    Replies: 1
    Last Post: 07-11-2002, 06:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21