Thread: Colour question

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Thumbs up Colour question

    Is it possible to colour the ENTIRE background of a console program, rather than just the background behind the text?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    Here's one way, I'll bet there are better ways. But this is the best I can do.

    Code:
    
    #include "stdafx.h"
    #include <windows.h>
    #include <iostream> 
    
    using namespace std; 
    
    HANDLE hStdout, hStdin; 
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
    BOOL SetBackForeColors(const HANDLE hConsole, const DWORD );
    
    int main(int argc, char* argv[])
    {
    	hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
    	if ( hStdout == INVALID_HANDLE_VALUE ) 
    	{
    		cout << "GetStdHandle" << endl; 
    		return 1;
    	}
    
    	SetBackForeColors( hStdout, ( FOREGROUND_GREEN | BACKGROUND_RED )  );
    
    	cout << "Ye ha..." << endl;
    
    	cout << endl << endl << endl << endl;
    
    	cout << "                           Yo-ho" << endl << endl;
    
    	return 0;
    }
    
    
    
    BOOL SetBackForeColors(const HANDLE hConsole, const DWORD dwColor ) 
    {   
    	COORD coordScreen = { 0, 0 };		/* here's where we'll home the cursor */   
    	BOOL bSuccess = TRUE;   
    	DWORD cCharsWritten;   
    	CONSOLE_SCREEN_BUFFER_INFO csbi;	/* to get buffer info */   
    	DWORD dwConSize;					/* number of character cells in the current buffer */    
    	
    	/* get the number of character cells in the current buffer */   
    	bSuccess = GetConsoleScreenBufferInfo(hConsole, &csbi);   
    	dwConSize = csbi.dwSize.X * csbi.dwSize.Y;   
    	
    
    	if ( ! SetConsoleTextAttribute( hStdout, dwColor  ) ) 
    		cout << "SetConsoleTextAttribute" << endl; 
    
    	/* fill the entire screen with blanks */   
    	bSuccess = FillConsoleOutputCharacter(hConsole, (TCHAR) ' ',  dwConSize, coordScreen, &cCharsWritten);   
    
    	/* get the current text attribute */   
    	if( bSuccess )
    		bSuccess = GetConsoleScreenBufferInfo(hConsole, &csbi);   
    	
    	/* now set the buffer's attributes accordingly */  
    	if( bSuccess )
    		bSuccess = FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);   
    	
    	/* put the cursor at (0, 0) */   
    	if( bSuccess )
    		bSuccess = SetConsoleCursorPosition(hConsole, coordScreen);   
    
    	return bSuccess; 
    }

  3. #3
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,039
    There is different ways of doing it. It all depends on your compiler. If your using MS Visual C++, the way Dang did it is the best. If you are using a DOS compiler, then it is simple. use
    textbackground(3); //or whatever number for a color

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  2. Replies: 5
    Last Post: 03-01-2003, 04:52 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. how to set the background colour of a static box?
    By Cobras2 in forum Windows Programming
    Replies: 2
    Last Post: 08-01-2002, 04:57 PM
  5. Colour theory... (weird title)
    By Magos in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2001, 04:16 PM