Thread: SetConsoleTextAttribute(Get..., int x)? How does it work

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    7

    SetConsoleTextAttribute(Get..., int x)? How does it work

    I have a function:
    Code:
    void color(int x)
    {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
    }
    I don't understand how the x works, when do I get the foreground and the background, I made a generator (for-loop to 1000 (and also a input color(...,int x)), I still didn't get it.

    Thanks.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    It actually takes type WORD instead of int.
    http://msdn.microsoft.com/library/en...ter_attributes
    The basic Red, Blue, and Green are defined already and you can mix them to make the other available console colors.

  3. #3
    Registered User Code Monkey's Avatar
    Join Date
    Jan 2006
    Location
    Denton
    Posts
    11
    BOOL SetConsoleTextAttribute(
    HANDLE hConsoleOutput,
    WORD wAttributes
    );


    HANDLE is the parameter that you pass in that is the handle to the output devices. For example the output device is the monitor.

    WORD is the data type that you pass in the color that you want to use, here is a list of colors for you:

    FOREGROUND_BLUE
    FOREGROUND_GREEN
    FOREGROUND_RED
    FOREGROUND_INTENSITY
    BACKGROUND_BLUE
    BACKGROUND_GREEN
    BACKGROUND_RED

    These colors are actualy hex vaules, but its easlier to just use the name instead of the hex value. To make other color values you bitwise OR the colors together

    Example: FOREGROUND_BLUE | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY

    This example gives you a text color of blue with a white background.
    Last edited by Code Monkey; 01-22-2006 at 12:47 PM.

  4. #4
    Registered User Code Monkey's Avatar
    Join Date
    Jan 2006
    Location
    Denton
    Posts
    11
    Here is an example program so you can better see how it works, remember use this tutorial to learn, not as a tool to copy and paste.

    Code:
    // Code by: Justin (Code Monkey)
    #include <windows.h>
    #include <iostream>
    #include <string>
    
    
    // This is our function that will change the text color of the text passed in
    void g_ColorText(std::string, WORD);
    
    int main() {
    	
    	g_ColorText("Hello World", FOREGROUND_BLUE | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
    
    	return 0;
    }
    
    // This is our function that will change the text color of the text passed in
    void g_ColorText(std::string text_message, WORD text_color) {
    	// This is our handle to our output device (monitor)
    	// But before we use the output_handle we need to initialize it,
    	// so we get the standard handle and set it to the standard output 
    	// handled device
    	HANDLE output_handle = GetStdHandle(STD_OUTPUT_HANDLE);
    
    	// This is our default color so that after the we change the text
    	// we can change it back to the default dos console color
    	WORD default_dos_color = FOREGROUND_RED | 
    							 FOREGROUND_GREEN | 
    							 FOREGROUND_BLUE | 
    							 FOREGROUND_INTENSITY;
    
    	// Here we pass in the output_handle and the text_color that we want
    	// the text to look like. This function changes the dos text colors.
    	// Its like as if yoou went to the DOS properties and change the colors
    	// mannualy. But this function lets you set the text attributes for the
    	// program so you don't have to tell the player/user of it to change
    	// the colors. = ]
    	SetConsoleTextAttribute(output_handle, text_color);
    
    	std::cout << text_message << std::endl;
    
    	// Simply sets every thing back to normal
    	SetConsoleTextAttribute(output_handle, default_dos_color);
    }

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Using the colour functions in a console is described in part 4 of my windows console programming tutorial starting here.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  4. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  5. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM