CPOsosky
asked on
Console Programming - System Color Command
Hi all.
I'm creating a console program using Visual c++ 6.0
I remember there being a system command that allowed
you to change the color of the background. Anyone know
the exact syntax? (I was unable to find it on msdn)
I believe it goes something like:
system("color 15");
although every combination I try gives an error.
-Chris
I'm creating a console program using Visual c++ 6.0
I remember there being a system command that allowed
you to change the color of the background. Anyone know
the exact syntax? (I was unable to find it on msdn)
I believe it goes something like:
system("color 15");
although every combination I try gives an error.
-Chris
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked, Thanks!
-Chris
-Chris
See also the sample at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/hh/winbase/conchar_2tid.asp ('Using the High-Level Input and Output Functions'):
#include <windows.h>
void NewLine(void);
void ScrollScreenBuffer(HANDLE,
HANDLE hStdout, hStdin;
CONSOLE_SCREEN_BUFFER_INFO
void main(void)
{
LPSTR lpszPrompt1 = "Type something and press Enter:\n";
LPSTR lpszPrompt2 = "Type any key: ";
CHAR chBuffer[256];
DWORD cRead, cWritten, fdwMode, fdwOldMode;
WORD wOldColorAttrs;
// Get handles to STDIN and STDOUT.
hStdin = GetStdHandle(STD_INPUT_HAN
hStdout = GetStdHandle(STD_OUTPUT_HA
if (hStdin == INVALID_HANDLE_VALUE ||
hStdout == INVALID_HANDLE_VALUE)
{
MyErrorExit("GetStdHandle"
}
// Save the current text colors.
if (! GetConsoleScreenBufferInfo
MyErrorExit("GetConsoleScr
wOldColorAttrs = csbiInfo.wAttributes;
// Set the text attr. to draw red text on black background.
if (! SetConsoleTextAttribute(hS
MyErrorExit("SetConsoleTex
// Write to STDOUT and read from STDIN by using the default
// modes. Input is echoed automatically, and ReadFile
// does not return until a carriage return is typed.
//
// The default input modes are line, processed, and echo.
// The default output modes are processed and wrap at EOL.
while (1)
{
if (! WriteFile(
hStdout, // output handle
lpszPrompt1, // prompt string
lstrlen(lpszPrompt1), // string length
&cWritten, // bytes written
NULL) ) // not overlapped
break;
if (! ReadFile(
hStdin, // input handle
chBuffer, // buffer to read into
255, // size of buffer
&cRead, // actual bytes read
NULL) ) // not overlapped
break;
if (chBuffer[0] == 'q') break;
}
// Turn off the line input mode, and echo the input mode.
if (! GetConsoleMode(hStdin, &fdwOldMode))
MyErrorExit("GetConsoleMod
fdwMode = fdwOldMode &
~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
if (! SetConsoleMode(hStdin, fdwMode))
MyErrorExit("SetConsoleMod
// Prompt for input.
if (! WriteFile(
hStdout, // output handle
lpszPrompt2, // prompt string
lstrlen(lpszPrompt2), // string length
&cWritten, // bytes written
NULL) ) // not overlapped
MyErrorExit("WriteFile");
// Without line and echo input modes, ReadFile returns
// when any input is available. Carriage returns must
// be handled, and WriteFile is used to echo input.
while (1)
{
if (! ReadFile(hStdin, chBuffer, 1, &cRead, NULL))
break;
if (chBuffer[0] == '\r')
NewLine();
else if (! WriteFile(hStdout, chBuffer, cRead,
&cWritten, NULL)) break;
if (chBuffer[0] == 'q') break;
}
// Restore the original console mode.
if (! SetConsoleMode(hStdin, fdwOldMode))
MyErrorExit("SetConsoleMod
// Restore the original text colors.
if (! SetConsoleTextAttribute(hS
MyErrorExit("SetConsoleTex
}
// The NewLine function handles carriage returns when the processed
// input mode is disabled. It gets the current cursor position
// and resets it to the first cell of the next row.
void NewLine(void)
{
if (! GetConsoleScreenBufferInfo
MyErrorExit("GetConsoleScr
csbiInfo.dwCursorPosition.
// If it is the last line in the screen buffer, scroll
// the buffer up.
if ((csbiInfo.dwSize.Y-1) == csbiInfo.dwCursorPosition.
{
ScrollScreenBuffer(hStdout
}
// Otherwise, advance the cursor to the next line.
else csbiInfo.dwCursorPosition.
if (! SetConsoleCursorPosition(h
csbiInfo.dwCursorPosition)
{
MyErrorExit("SetConsoleCur
}
}