CodePudding user response:
For reference only# include & lt; Windows. H>
# include & lt; Stdio. H>
Void ConPrint (char * CharBuffer, int len);
Void ConPrintAt (int x, int y, char * CharBuffer, int len);
Void gotoXY (int x, int y);
Void ClearConsole (void);
Void ClearConsoleToColors (int ForgC, int BackC);
Void SetColorAndBackground (int ForgC, int BackC);
Void SetColor (int ForgC);
Void HideTheCursor (void);
Void ShowTheCursor (void);
Int main (int arg c, char * argv [])
{
HideTheCursor ();
ClearConsoleToColors (15, 1);
ClearConsole ();
GotoXY (1, 1);
SetColor (14);
Printf (" This is a test... \n");
Sleep (5000);
ShowTheCursor ();
SetColorAndBackground (15, 12);
ConPrint (" This is also a test... \ n ", 23);
SetColorAndBackground (1, 7);
ConPrintAt (22, 15, "This is also a test... \ n ", 23);
GotoXY (0, 24);
SetColorAndBackground (7, 1);
return 0;
}
//This will clear the console while setting the forground and
//background colors.
Void ClearConsoleToColors (int ForgC, int BackC)
{
WORD wColor=((BackC & amp; 0 x0f) & lt; <4) + (ForgC & amp; 0 x0f);
//Get the handle to the current output buffer...
HANDLE hStdOut=GetStdHandle (STD_OUTPUT_HANDLE);
//This is 2 reset the carat/cursor to the top left.
COORD COORD={0, 0};
//A return value... Indicating how many chars were written
//not 2 but we need to capture this since it will be
//written anyway (passing NULL causes an access violation).
DWORD count;
//This is a structure containing all of the console info
//it is 2 here to find the size of the console.
CONSOLE_SCREEN_BUFFER_INFO csbi;
//Here we will set the current color
SetConsoleTextAttribute (hStdOut, wColor);
If (GetConsoleScreenBufferInfo (hStdOut, & amp; Csbi))
{
//This fills the buffer with a given character (in This case 32=space).
FillConsoleOutputCharacter (hStdOut, (TCHAR) 32, csbi dwSize. X * csbi dwSize. J Y, coord, & amp; The count);
FillConsoleOutputAttribute (hStdOut, csbi. WAttributes, csbi. DwSize. X * csbi dwSize. J Y, coord, & amp; The count);
//This will set our cursor position for the next print statement.
SetConsoleCursorPosition (hStdOut, coord);
}
}
//This will clear the console.
Void ClearConsole ()
{
//Get the handle to the current output buffer...
HANDLE hStdOut=GetStdHandle (STD_OUTPUT_HANDLE);
//This is 2 reset the carat/cursor to the top left.
COORD COORD={0, 0};
//A return value... Indicating how many chars were written
//not 2 but we need to capture this since it will be
//written anyway (passing NULL causes an access violation).
DWORD count;
//This is a structure containing all of the console info
//it is 2 here to find the size of the console.
CONSOLE_SCREEN_BUFFER_INFO csbi;
//Here we will set the current color
If (GetConsoleScreenBufferInfo (hStdOut, & amp; Csbi))
{
//This fills the buffer with a given character (in This case 32=space).
FillConsoleOutputCharacter (hStdOut, (TCHAR) 32, csbi dwSize. X * csbi dwSize. J Y, coord, & amp; The count);
FillConsoleOutputAttribute (hStdOut, csbi. WAttributes, csbi. DwSize. X * csbi dwSize. J Y, coord, & amp; The count);
//This will set our cursor position for the next print statement.
SetConsoleCursorPosition (hStdOut, coord);
}
}
//This will set the position of the cursor
Void gotoXY (int x, int y)
{
//Initialize the coordinates
COORD COORD={x, y};
//Set the position
SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), coord);
}
//This will set the forground color for printing in a console window.
Void SetColor (int ForgC)
{
WORD wColor;
//We will need this handle to get the current background attribute
HANDLE hStdOut=GetStdHandle (STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
//We use csbi for wAttributes word.
If (GetConsoleScreenBufferInfo (hStdOut, & amp; Csbi))
{
//Mask out all but the background attribute, and add in the forgournd color
WColor=(csbi wAttributes & amp; 0 xf0) + (ForgC & amp; 0 x0f);
SetConsoleTextAttribute (hStdOut, wColor);
}
}
//This will set the forground and background color for printing in a console window.
Void SetColorAndBackground (int ForgC, int BackC)
{
WORD wColor=((BackC & amp; 0 x0f) & lt; <4) + (ForgC & amp; 0 x0f);;
SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), wColor);
}
//Direct console output
Void ConPrint (char * CharBuffer, int len)
{
DWORD count;
WriteConsole (GetStdHandle (STD_OUTPUT_HANDLE), CharBuffer, len, & amp; The count, NULL);
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull