Is it possible to colour the ENTIRE background of a console program, rather than just the background behind the text?
This is a discussion on Colour question within the C++ Programming forums, part of the General Programming Boards category; Is it possible to colour the ENTIRE background of a console program, rather than just the background behind the text?...
Is it possible to colour the ENTIRE background of a console program, rather than just the background behind the text?
Please direct all complaints regarding this post to the nearest brick wallHave a nice day.
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; }
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