Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

WriteConsoleOutput (kernel32)
 
.
Summary

C# Signature:

    /* Writes character and color attribute data to a specified rectangular block of character cells in a console screen buffer.
        The data to be written is taken from a correspondingly sized rectangular block at a specified location in the source buffe */
    [DllImport("kernel32.dll", EntryPoint = "WriteConsoleOutputW", CharSet = CharSet.Unicode, SetLastError = true)]
    internal static extern BOOL WriteConsoleOutput(
        SafeConsoleHandle hConsoleOutput,
        /* This pointer is treated as the origin of a two-dimensional array of CHAR_INFO structures
        whose size is specified by the dwBufferSize parameter.*/
        [MarshalAs(UnmanagedType.LPArray), In] CHAR_INFO[,] lpBuffer,
        COORD dwBufferSize,
        COORD dwBufferCoord,
        ref SMALL_RECT lpWriteRegion);

User-Defined Types:

    public enum CharAttributes : ushort
    {
    /// <summary>
    /// None.
    /// </summary>
    None = 0x0000,

None.

    /// <summary>
    /// Text color contains blue.
    /// </summary>
    FOREGROUND_BLUE = 0x0001,

Notes:

None.

    /// <summary>
    /// Text color contains green.
    /// </summary>
    FOREGROUND_GREEN = 0x0002,

Tips & Tricks:

Please add some!

    /// <summary>
    /// Text color contains red.
    /// </summary>
    FOREGROUND_RED = 0x0004,

Sample Code:

    class Program
    {
    static void Main()
    {
        using (var cb = new Console())
        {
        var ainfoArray = new ConsoleApi.CHAR_INFO[4, 10];
        for (int e = 0; e < 4; e++)
        {
            for (int i = 0; i < 10; i++)
            {
            switch (e)
            {
                case 0:
                ainfoArray[e, i] = new ConsoleApi.CHAR_INFO
                {
                    UnicodeChar = 'a',
                    Attributes = ConsoleApi.Attribute.Gray
                };
                break;

    /// <summary>
    /// Text color is intensified.
    /// </summary>
    FOREGROUND_INTENSITY = 0x0008,
                case 1:
                ainfoArray[e, i] = new ConsoleApi.CHAR_INFO
                {
                    UnicodeChar = 'b',
                    Attributes = ConsoleApi.Attribute.Red
                };
                break;

    /// <summary>
    /// Background color contains blue.
    /// </summary>
    BACKGROUND_BLUE = 0x0010,
                case 2:
                ainfoArray[e, i] = new ConsoleApi.CHAR_INFO
                {
                    UnicodeChar = 'c',
                    Attributes = ConsoleApi.Attribute.SkyBlue
                };
                break;

    /// <summary>
    /// Background color contains green.
    /// </summary>
    BACKGROUND_GREEN = 0x0020,
                case 3:
                ainfoArray[e, i] = new ConsoleApi.CHAR_INFO
                {
                    UnicodeChar = 'd',
                    Attributes = ConsoleApi.Attribute.Yellow
                };
                break;
            }
            }
        }

    /// <summary>
    /// Background color contains red.
    /// </summary>
    BACKGROUND_RED = 0x0040,
        short
            xI = 0,
            yI = 0;

        short
            xII = 10,
            yII = 4;

    /// <summary>
    /// Background color is intensified.
    /// </summary>
    BACKGROUND_INTENSITY = 0x0080,
        short
            xx = (SHORT) (xI + xII - 1),
            yy = (SHORT) (yI + yII - 1);

    /// <summary>
    /// Leading byte.
    /// </summary>
    COMMON_LVB_LEADING_BYTE = 0x0100,
        var from = new ConsoleApi.COORD() { X = xI, Y = yI };
        var end = new ConsoleApi.COORD() { X = xII, Y = yII };
        var position = new ConsoleApi.SMALL_RECT() { Left = xI, Right = xx, Top = yI, Bottom = yy };
        if (ConsoleApi.WriteConsoleOutput(
            cb.StdOutput, ainfoArray, end, from, ref position))
        {
            cb.ReadLine();
        }
        }
    }
    }

Alternative Managed API:

Do you know one? Please contribute it!

    /// <summary>
    /// Trailing byte.
    /// </summary>
    COMMON_LVB_TRAILING_BYTE = 0x0200,

Documentation

    /// <summary>
    /// Top horizontal
    /// </summary>
    COMMON_LVB_GRID_HORIZONTAL = 0x0400,

    /// <summary>
    /// Left vertical.
    /// </summary>
    COMMON_LVB_GRID_LVERTICAL = 0x0800,

    /// <summary>
    /// Right vertical.
    /// </summary>
    COMMON_LVB_GRID_RVERTICAL = 0x1000,

    /// <summary>
    /// Reverse foreground and background attribute.
    /// </summary>
    COMMON_LVB_REVERSE_VIDEO = 0x4000,

    /// <summary>
    /// Underscore.
    /// </summary>
    COMMON_LVB_UNDERSCORE = 0x8000,
    }

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

    class Program
    {
    static void Main()
    {
        using (var cb = new Console())
        {
        var ainfoArray = new ConsoleApi.CHAR_INFO[4, 10];
        for (int e = 0; e < 4; e++)
        {
            for (int i = 0; i < 10; i++)
            {
            switch (e)
            {
                case 0:
                ainfoArray[e, i] = new ConsoleApi.CHAR_INFO
                {
                    UnicodeChar = 'a',
                    Attributes = ConsoleApi.Attribute.Gray
                };
                break;

                case 1:
                ainfoArray[e, i] = new ConsoleApi.CHAR_INFO
                {
                    UnicodeChar = 'b',
                    Attributes = ConsoleApi.Attribute.Red
                };
                break;

                case 2:
                ainfoArray[e, i] = new ConsoleApi.CHAR_INFO
                {
                    UnicodeChar = 'c',
                    Attributes = ConsoleApi.Attribute.SkyBlue
                };
                break;

                case 3:
                ainfoArray[e, i] = new ConsoleApi.CHAR_INFO
                {
                    UnicodeChar = 'd',
                    Attributes = ConsoleApi.Attribute.Yellow
                };
                break;
            }
            }
        }

        short
            xI = 0,
            yI = 0;

        short
            xII = 10,
            yII = 4;

        short
            xx = (SHORT) (xI + xII - 1),
            yy = (SHORT) (yI + yII - 1);

        var from = new ConsoleApi.COORD() { X = xI, Y = yI };
        var end = new ConsoleApi.COORD() { X = xII, Y = yII };
        var position = new ConsoleApi.SMALL_RECT() { Left = xI, Right = xx, Top = yI, Bottom = yy };
        if (ConsoleApi.WriteConsoleOutput(
            cb.StdOutput, ainfoArray, end, from, ref position))
        {
            cb.ReadLine();
        }
        }
    }
    }

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions