Hi I'm trying to make an output with WriteConsoleOutputA.
I have this code:
CHAR_INFO letterA;
letterA.Char.AsciiChar = 'A';
letterA.Attributes =
FOREGROUND_RED | FOREGROUND_INTENSITY |
BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_INTENSITY;
//Set up the positions:
COORD charBufSize = { 1, 1};
COORD characterPos = { 0, 0 };
SMALL_RECT writeArea = { 0,0,0,0 };
//Write the character
WriteConsoleOutputA(wHnd, &letterA, charBufSize, characterPos, &writeArea);
So at this point it writes a red A with a yellow background, but for example, if I want the A appear in the coordinates (5,5) it doesn't print it even if I change SMALL_RECT to {0, 0, 10, 10}.
Or if I want to write another A right side to the first one with this:
WriteConsoleOutputA(wHnd, &letterA, charBufSize, characterPos, &writeArea);
WriteConsoleOutputA(wHnd, &letterA, charBufSize, { 0, 1 }, &writeArea);
I´m beginning with this graphical console mode, it would be very helpful if someone could tell me how to print that character in the coordinate that I want.
I have tried to change it , changing the coordinates something like this:
COORD charBufSize = { 5, 10};
COORD characterPos = { 3, 2 };
SMALL_RECT writeArea = { 0,0,5,10 };
but it prints weird characters and other colours in all the buffer 5*10.
Thanks
César.
WriteConsoleOutput(..) is a complex function which needs to be handled carefully.
The dwBufferSize parameter (= your charBufSize) is nothing more than a size specification of the lpBuffer parameter (= your letterA). The only difference instead of simply telling that letterA has a size of 1 is that by splitting it into two axis you are able specify the width and height of a text block with letterA characters in it. But remember that the size of letterA has to be charBufSize.X * charBufSize.Y. Otherwise WriteConsoleOutput will do weird stuff since it uses uninitialized memory.
The dwBufferCoord parameter (= your characterPos) defines the location within letterA from where to read the characters to be written to the console. So it simply defines an index offset. In your example this should always be { 0, 0 } (which is equal to letterA[0]) since letterA is only a single character.
The lpWriteRegion parameter (= your writeArea) does all the magic. It specifies the position, width and height of the area to be written by the call. The data to be written is definedby the previous parameters.
So to write a character to a specific location x, y do the following:
COORD charBufSize = {1, 1};
COORD characterPos = {0, 0};
SMALL_RECT writeArea = {x, y, x, y};
WriteConsoleOutputA(wHnd, &letterA, charBufSize, characterPos, &writeArea);
For a little better understanding use the following example and play a little with the values of charBufSize, characterPos and writeArea:
int i;
CHAR_INFO charInfo[10 * 10];
/* play with these values */
COORD charBufSize = {10, 10}; /* do not exceed x*y=100 !!! */
COORD characterPos = {5, 0}; /* must be within 0 and x*y=100 */
SMALL_RECT writeArea = {2, 2, 12, 12};
for (i = 0; i < (10 * 10); i++)
{
charInfo[i].Char.AsciiChar = 'A' + (i % 26);
charInfo[i].Attributes = FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_INTENSITY;
}
WriteConsoleOutputA(wHnd, charInfo, charBufSize, characterPos, &writeArea);
Here is a screenshot of the parameters in the example above showing the console and the variables. I hope this makes it a bit more clear.

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With