Writing Characters or Colors to Consecutive Cells

Characters or color attributes can be written to specified character cells in a screen buffer. The following example uses the WriteConsoleOutputCharacter function to write a string of characters beginning at the upper left corner of a screen buffer. Then the example uses the WriteConsoleOutputAttribute function to write a string of color attributes to the first 51 cells of the same row. The coord parameter for both functions specifies the character cell in the screen buffer at which writing begins. The location in the console window where these characters or colors appear depends on the current window rectangle of the screen buffer. For additional information about the relationship between a screen buffer and its windows, see Window and Screen Buffer Size and Scrolling the Screen Buffer.

HANDLE hOutput; 
    LPTSTR lpszString = "Character String"; 
    DWORD cWritten; 
    BOOL fSuccess; 
    COORD coord; 
    WORD wColors[3], wColor; 
    CHAR chFillChar; 
 
// Write a string of characters to a screen buffer. 
 
    coord.X = 0;            // start at first cell 
    coord.Y = 0;            //   of first row      
    fSuccess = WriteConsoleOutputCharacter( 
        hOutput,              // screen buffer handle 
        lpszString,           // pointer to source string 
        lstrlen(lpszString),  // length of string 
        coord,                // first cell to write to 
        &cWritten);           // actual number written 
    if (! fSuccess) 
        MyErrorExit("WriteConsoleOutputCharacter"); 
 
// Write a string of colors to a screen buffer. 
 
    wColors[0] = BACKGROUND_RED; 
    wColors[1] = BACKGROUND_RED |     // white background 
                 BACKGROUND_GREEN | 
                 BACKGROUND_BLUE; 
    wColors[2] = BACKGROUND_BLUE; 

    for (;fSuccess && coord.X < 50; coord.X += 3) 
    {
        fSuccess = WriteConsoleOutputAttribute( 
            hOutput,          // screen buffer handle 
            wColors,          // pointer to source string 
            3,                // length of string 
            coord,            // first cell to write to 
            &cWritten);       // actual number written 
    }
    if (! fSuccess) 
        MyErrorExit("WriteConsoleOutputAttribute"); 
 

The same character or color attribute can be written to a specified number of consecutive screen buffer cells beginning at a specified location. The following example uses the FillConsoleOutputCharacter function to clear a 80-by-50-character screen buffer, and then it uses the FillConsoleOutputAttribute function to set the color attributes of the same cells.

// Fill an 80-by-50-character screen buffer with the space character. 
 
    coord.X = 0;            // start at first cell 
    coord.Y = 0;            //   of first row 
    chFillChar = ' '; 
 
    fSuccess = FillConsoleOutputCharacter( 
        hStdout,          // screen buffer handle 
        chFillChar,       // fill with spaces 
        80*50,            // number of cells to fill 
        coord,            // first cell to write to 
        &cWritten);       // actual number written 

    if (! fSuccess) 
        MyErrorExit("FillConsoleOutputCharacter"); 
 
// Set 80-by-50-character screen buffer colors to white text on red. 
 
    wColor = BACKGROUND_RED | 
             FOREGROUND_RED | 
             FOREGROUND_GREEN | 
             FOREGROUND_BLUE; 

    fSuccess = FillConsoleOutputAttribute( 
        hStdout,          // screen buffer handle 
        wColor,           // color to fill with 
        80*50,            // number of cells to fill 
        coord,            // first cell to write to 
        &cWritten);       // actual number written 

    if (! fSuccess) 
        MyErrorExit("FillConsoleOutputAttribute");