| 1 |
/* |
| 2 |
* Demonstrates that console clearing sets each cell's character to SP, not |
| 3 |
* NUL, and it sets the attribute of each cell to the current text attribute. |
| 4 |
* |
| 5 |
* This confirms the MSDN instruction in the "Clearing the Screen" article. |
| 6 |
* https://msdn.microsoft.com/en-us/library/windows/desktop/ms682022(v=vs.85).aspx |
| 7 |
* It advises using GetConsoleScreenBufferInfo to get the current text |
| 8 |
* attribute, then FillConsoleOutputCharacter and FillConsoleOutputAttribute to |
| 9 |
* write to the console buffer. |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <windows.h> |
| 13 |
|
| 14 |
#include <cassert> |
| 15 |
#include <cstdio> |
| 16 |
#include <cstdlib> |
| 17 |
|
| 18 |
#include "TestUtil.cc" |
| 19 |
|
| 20 |
int main(int argc, char *argv[]) { |
| 21 |
if (argc == 1) { |
| 22 |
startChildProcess(L"CHILD"); |
| 23 |
return 0; |
| 24 |
} |
| 25 |
|
| 26 |
const HANDLE conout = GetStdHandle(STD_OUTPUT_HANDLE); |
| 27 |
|
| 28 |
SetConsoleTextAttribute(conout, 0x24); |
| 29 |
system("cls"); |
| 30 |
|
| 31 |
setWindowPos(0, 0, 1, 1); |
| 32 |
setBufferSize(80, 25); |
| 33 |
setWindowPos(0, 0, 80, 25); |
| 34 |
|
| 35 |
CHAR_INFO buf; |
| 36 |
COORD bufSize = { 1, 1 }; |
| 37 |
COORD bufCoord = { 0, 0 }; |
| 38 |
SMALL_RECT rect = { 5, 5, 5, 5 }; |
| 39 |
BOOL ret; |
| 40 |
DWORD actual; |
| 41 |
COORD writeCoord = { 5, 5 }; |
| 42 |
|
| 43 |
// After cls, each cell's character is a space, and its attributes are the |
| 44 |
// default text attributes. |
| 45 |
ret = ReadConsoleOutputW(conout, &buf, bufSize, bufCoord, &rect); |
| 46 |
assert(ret && buf.Char.UnicodeChar == L' ' && buf.Attributes == 0x24); |
| 47 |
|
| 48 |
// Nevertheless, it is possible to change a cell to NUL. |
| 49 |
ret = FillConsoleOutputCharacterW(conout, L'\0', 1, writeCoord, &actual); |
| 50 |
assert(ret && actual == 1); |
| 51 |
ret = ReadConsoleOutputW(conout, &buf, bufSize, bufCoord, &rect); |
| 52 |
assert(ret && buf.Char.UnicodeChar == L'\0' && buf.Attributes == 0x24); |
| 53 |
|
| 54 |
// As well as a 0 attribute. (As one would expect, the cell is |
| 55 |
// black-on-black.) |
| 56 |
ret = FillConsoleOutputAttribute(conout, 0, 1, writeCoord, &actual); |
| 57 |
assert(ret && actual == 1); |
| 58 |
ret = ReadConsoleOutputW(conout, &buf, bufSize, bufCoord, &rect); |
| 59 |
assert(ret && buf.Char.UnicodeChar == L'\0' && buf.Attributes == 0); |
| 60 |
ret = FillConsoleOutputCharacterW(conout, L'X', 1, writeCoord, &actual); |
| 61 |
assert(ret && actual == 1); |
| 62 |
ret = ReadConsoleOutputW(conout, &buf, bufSize, bufCoord, &rect); |
| 63 |
assert(ret && buf.Char.UnicodeChar == L'X' && buf.Attributes == 0); |
| 64 |
|
| 65 |
// The 'X' is invisible. |
| 66 |
countDown(3); |
| 67 |
|
| 68 |
ret = FillConsoleOutputAttribute(conout, 0x42, 1, writeCoord, &actual); |
| 69 |
assert(ret && actual == 1); |
| 70 |
|
| 71 |
countDown(5); |
| 72 |
} |