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;
}