public void MoveBufferArea(int sourceLeft, int sourceTop, int sourceWidth, int sourceHeight,
                                   int targetLeft, int targetTop, Char sourceChar,
                                   ConsoleColor sourceForeColor, ConsoleColor sourceBackColor)
        {
            if (sourceForeColor < 0)
            {
                throw new ArgumentException("Cannot be less than 0.", "sourceForeColor");
            }

            if (sourceBackColor < 0)
            {
                throw new ArgumentException("Cannot be less than 0.", "sourceBackColor");
            }

            if (sourceWidth == 0 || sourceHeight == 0)
            {
                return;
            }

            ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo();

            GetConsoleScreenBufferInfo(outputHandle, out info);
            CharInfo[] buffer = new CharInfo[sourceWidth * sourceHeight];
            Coord      bsize  = new Coord(sourceWidth, sourceHeight);
            Coord      bpos   = new Coord(0, 0);
            SmallRect  region = new SmallRect(sourceLeft, sourceTop, sourceLeft + sourceWidth - 1, sourceTop + sourceHeight - 1);

            fixed(void *ptr = &buffer[0])
            {
                if (!ReadConsoleOutput(outputHandle, ptr, bsize, bpos, ref region))
                {
                    throw new ArgumentException(String.Empty, "Cannot read from the specified coordinates.");
                }
            }

            int   written;
            short attr = GetAttrForeground(0, sourceForeColor);

            attr = GetAttrBackground(attr, sourceBackColor);
            bpos = new Coord(sourceLeft, sourceTop);
            for (int i = 0; i < sourceHeight; i++, bpos.Y++)
            {
                FillConsoleOutputCharacter(outputHandle, sourceChar, sourceWidth, bpos, out written);
                FillConsoleOutputAttribute(outputHandle, attr, sourceWidth, bpos, out written);
            }

            bpos   = new Coord(0, 0);
            region = new SmallRect(targetLeft, targetTop, targetLeft + sourceWidth - 1, targetTop + sourceHeight - 1);
            if (!WriteConsoleOutput(outputHandle, buffer, bsize, bpos, ref region))
            {
                throw new ArgumentException(String.Empty, "Cannot write to the specified coordinates.");
            }
        }
        public void SetWindowSize(int width, int height)
        {
            ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo();

            GetConsoleScreenBufferInfo(outputHandle, out info);
            SmallRect rect = info.Window;

            rect.Right  = (short)(rect.Left + width - 1);
            rect.Bottom = (short)(rect.Top + height - 1);
            if (!SetConsoleWindowInfo(outputHandle, true, ref rect))
            {
                throw new ArgumentOutOfRangeException("left/top", "Windows error " + Marshal.GetLastWin32Error());
            }
        }
        public void SetWindowPosition(int left, int top)
        {
            ConsoleScreenBufferInfo info = new ConsoleScreenBufferInfo();

            GetConsoleScreenBufferInfo(outputHandle, out info);
            SmallRect rect = info.Window;

            rect.Left = (short)left;
            rect.Top  = (short)top;
            if (!SetConsoleWindowInfo(outputHandle, true, ref rect))
            {
                throw new ArgumentOutOfRangeException("left/top", "Windows error " + Marshal.GetLastWin32Error());
            }
        }
 extern static bool WriteConsoleOutput(IntPtr handle, CharInfo[] buffer, Coord bsize, Coord bpos, ref SmallRect region);
 extern static bool ReadConsoleOutput(IntPtr handle, void *buffer, Coord bsize, Coord bpos, ref SmallRect region);
 extern static bool SetConsoleWindowInfo(IntPtr handle, bool absolute, ref SmallRect rect);