예제 #1
0
파일: Crt.cs 프로젝트: HolodeckOne/RMLib
        /// <summary>
        /// Writes a string of text at the desired X/Y coordinate with the given text attribute.
        /// 
        /// FastWrite is not window-relative, and it does not wrap text that goes beyond the right edge of the screen.
        /// </summary>
        /// <param name="text">The text to write</param>
        /// <param name="column">The 1-based column to start the text</param>
        /// <param name="row">The 1-based row to start the text</param>
        /// <param name="attribute">The text attribute to colour the text</param>
        public static void FastWrite(string text, int column, int row, int attribute)
        {
            lock (_Lock)
            {
                if ((column <= _ScreenSize.X) && (row <= _ScreenSize.Y))
                {
                    if (OSUtils.IsWindows)
                    {
                        NativeMethods.CHAR_INFO[] Buffer = new NativeMethods.CHAR_INFO[text.Length];
                        for (int i = 0; i < text.Length; i++)
                        {
                            Buffer[i].Attributes = (ushort)attribute;
                            Buffer[i].AsciiChar = text[i];
                        }

                        NativeMethods.COORD BufferCoord = new NativeMethods.COORD();
                        BufferCoord.X = 0;
                        BufferCoord.Y = 0;

                        NativeMethods.COORD BufferSize = new NativeMethods.COORD();
                        BufferSize.X = (short)text.Length;
                        BufferSize.Y = 1;

                        NativeMethods.SMALL_RECT WriteRegion = new NativeMethods.SMALL_RECT();
                        WriteRegion.Bottom = (short)(row - 1);
                        WriteRegion.Left = (short)(column - 1);
                        WriteRegion.Right = (short)((column - 1) + text.Length);
                        WriteRegion.Top = (short)(row - 1);

                        NativeMethods.WriteConsoleOutput(_StdOutputHandle, Buffer, BufferSize, BufferCoord, ref WriteRegion);
                    }
                    else
                    {
                        try
                        {
                            int OldX = Console.CursorLeft;
                            int OldY = Console.CursorTop;
                            ConsoleColor OldFore = Console.ForegroundColor;
                            ConsoleColor OldBack = Console.BackgroundColor;

                            Console.SetCursorPosition(column - 1, row - 1);
                            //Console.ForegroundColor =
                            GetConsoleColour(attribute % 16);
                            //Console.BackgroundColor =
                            GetConsoleColour(attribute / 16);

                            Console.Write(text);
                            // TODO Keep text in buffer for SaveScreen() and RestoreScreen()

                            Console.SetCursorPosition(OldX, OldY);
                            Console.ForegroundColor = OldFore;
                            Console.BackgroundColor = OldBack;
                        }
                        catch (NotImplementedException)
                        {
                            // Can't do that
                        }

                    }
                }
            }
        }
예제 #2
0
파일: Crt.cs 프로젝트: HolodeckOne/RMLib
        /// <summary>
        /// Scrolls the given window up the given number of lines (leaving blank lines at the bottom), filling the void with the given character with the given text attribute
        /// </summary>
        /// <param name="AX1">The 0-based left column of the window</param>
        /// <param name="AY1">The 0-based top row of the window</param>
        /// <param name="AX2">The 0-based right column of the window</param>
        /// <param name="AY2">The 0-based bottom row of the window</param>
        /// <param name="ALines">The number of lines to scroll</param>
        /// <param name="ACh">The character to fill the void with</param>
        /// <param name="AAttr">The text attribute to fill the void with</param>
        private static void ScrollUpCustom(int AX1, int AY1, int AX2, int AY2, int ALines, char ACh, int AAttr)
        {
            lock (_Lock)
            {
                if (OSUtils.IsWindows)
                {
                    NativeMethods.SMALL_RECT ClipRectangle = new NativeMethods.SMALL_RECT();
                    ClipRectangle.Bottom = (short)AY2;
                    ClipRectangle.Left = (short)AX1;
                    ClipRectangle.Right = (short)AX2;
                    ClipRectangle.Top = (short)AY1;

                    NativeMethods.CHAR_INFO Fill = new NativeMethods.CHAR_INFO();
                    Fill.AsciiChar = ACh;
                    Fill.Attributes = (ushort)AAttr;

                    NativeMethods.SMALL_RECT ScrollRectangle = ClipRectangle;

                    NativeMethods.COORD DestinationOrigin = new NativeMethods.COORD();
                    DestinationOrigin.X = (short)AX1;
                    DestinationOrigin.Y = (short)(AY1 - ALines);
                    NativeMethods.ScrollConsoleScreenBuffer(_StdOutputHandle, ref ScrollRectangle, ref ClipRectangle, DestinationOrigin, ref Fill);
                }
                else
                {
                    try
                    {
                        Console.MoveBufferArea(AX1, AY1, AX2, AY2, AX1, AY1 - ALines, ' ', GetConsoleColour(_TextAttr % 16), GetConsoleColour(_TextAttr / 16));
                    }
                    catch (NotImplementedException)
                    {
                        try
                        {
                            int OldX = Console.CursorLeft;
                            int OldY = Console.CursorTop;

                            Console.SetCursorPosition(0, _ScreenSize.Y - 1);
                            for (int i = 0; i < ALines; i++) Console.WriteLine();

                            Console.SetCursorPosition(OldX, OldY);
                        }
                        catch (NotImplementedException)
                        {
                            // Can't do that
                        }
                    }
                }
            }
        }
예제 #3
0
파일: Crt.cs 프로젝트: HolodeckOne/RMLib
        /// <summary>
        /// Scrolls the given window down the given number of lines (leaving blank lines at the top), filling the void with the given character with the given text attribute
        /// </summary>
        /// <param name="AX1">The 0-based left column of the window</param>
        /// <param name="AY1">The 0-based top row of the window</param>
        /// <param name="AX2">The 0-based right column of the window</param>
        /// <param name="AY2">The 0-based bottom row of the window</param>
        /// <param name="ALines">The number of lines to scroll</param>
        /// <param name="ACh">The character to fill the void with</param>
        /// <param name="AAttr">The text attribute to fill the void with</param>
        private static void ScrollDownCustom(int AX1, int AY1, int AX2, int AY2, int ALines, char ACh, int AAttr)
        {
            lock (_Lock)
            {
                if (OSUtils.IsWindows)
                {
                    NativeMethods.COORD DestinationOrigin = new NativeMethods.COORD();
                    DestinationOrigin.X = (short)AX1;
                    DestinationOrigin.Y = (short)(AY1 + ALines);

                    NativeMethods.CHAR_INFO Fill = new NativeMethods.CHAR_INFO();
                    Fill.AsciiChar = ACh;
                    Fill.Attributes = (ushort)AAttr;

                    NativeMethods.SMALL_RECT ScrollRectangle = new NativeMethods.SMALL_RECT();
                    ScrollRectangle.Bottom = (short)(AY2 - ALines);
                    ScrollRectangle.Left = (short)AX1;
                    ScrollRectangle.Right = (short)AX2;
                    ScrollRectangle.Top = (short)AY1;

                    NativeMethods.ScrollConsoleScreenBuffer(_StdOutputHandle, ref ScrollRectangle, IntPtr.Zero, DestinationOrigin, ref Fill);
                }
                else
                {
                    try
                    {
                        Console.MoveBufferArea(AX1, AY1, AX2, AY2 - ALines, AX1, AY1 + ALines, ' ', GetConsoleColour(_TextAttr % 16), GetConsoleColour(_TextAttr / 16));
                    }
                    catch (NotImplementedException)
                    {
                        // Can't do that
                    }
                }
            }
        }
예제 #4
0
파일: Crt.cs 프로젝트: HolodeckOne/RMLib
        public static void SetWindowSize(int columns, int rows)
        {
            lock (_Lock)
            {
                // Set the new console screen buffer size
                _ScreenSize.X = (short)columns;
                _ScreenSize.Y = (short)rows;
                if (OSUtils.IsWindows)
                {
                    NativeMethods.SetConsoleScreenBufferSize(_StdOutputHandle, _ScreenSize);
                }
                else
                {
                    try
                    {
                        Console.SetBufferSize(_ScreenSize.X, _ScreenSize.Y);
                    }
                    catch (NotImplementedException)
                    {
                        // Can't do that
                    }
                }

                // And make the actual window size match the new buffer size
                NativeMethods.SMALL_RECT ConsoleWindow = new NativeMethods.SMALL_RECT();
                ConsoleWindow.Left = 0;
                ConsoleWindow.Top = 0;
                ConsoleWindow.Right = (short)(columns - 1);
                ConsoleWindow.Bottom = (short)(rows - 1);
                if (OSUtils.IsWindows)
                {
                    NativeMethods.SetConsoleWindowInfo(_StdOutputHandle, true, ref ConsoleWindow);
                }
                else
                {
                    try
                    {
                        Console.SetWindowSize(_ScreenSize.X, _ScreenSize.Y);
                    }
                    catch (NotImplementedException)
                    {
                        // Can't do that
                    }

                }

                // Update the WindMin/WindMax records
                WindMin = 0;
                WindMax = (_ScreenSize.X - 1) | ((_ScreenSize.Y - 1) << 8);
            }
        }
예제 #5
0
파일: Crt.cs 프로젝트: HolodeckOne/RMLib
        public static NativeMethods.CHAR_INFO[] SaveScreen(int left, int top, int right, int bottom)
        {
            lock (_Lock)
            {
                if (OSUtils.IsWindows)
                {
                    NativeMethods.COORD BufferSize = new NativeMethods.COORD();
                    BufferSize.X = (short)(right - left + 1);
                    BufferSize.Y = (short)(bottom - top + 1);

                    NativeMethods.CHAR_INFO[] Buffer = new NativeMethods.CHAR_INFO[BufferSize.X * BufferSize.Y];

                    NativeMethods.COORD BufferCoord = new NativeMethods.COORD();
                    BufferCoord.X = 0;
                    BufferCoord.Y = 0;

                    NativeMethods.SMALL_RECT ReadRegion = new NativeMethods.SMALL_RECT();
                    ReadRegion.Left = (short)(left - 1);
                    ReadRegion.Top = (short)(top - 1);
                    ReadRegion.Right = (short)(right - 1);
                    ReadRegion.Bottom = (short)(bottom - 1);

                    NativeMethods.ReadConsoleOutput(_StdOutputHandle, Buffer, BufferSize, BufferCoord, ref ReadRegion);
                    return Buffer;
                }
                else
                {
                    // TODO Save from buffer
                    return null;
                }
            }
        }
예제 #6
0
파일: Crt.cs 프로젝트: HolodeckOne/RMLib
        public static void RestoreScreen(NativeMethods.CHAR_INFO[] buffer, int left, int top, int right, int bottom)
        {
            lock (_Lock)
            {
                if (OSUtils.IsWindows)
                {
                    NativeMethods.COORD BufferCoord = new NativeMethods.COORD();
                    BufferCoord.X = 0;
                    BufferCoord.Y = 0;

                    NativeMethods.COORD BufferSize = new NativeMethods.COORD();
                    BufferSize.X = (short)(right - left + 1);
                    BufferSize.Y = (short)(bottom - top + 1);

                    NativeMethods.SMALL_RECT WriteRegion = new NativeMethods.SMALL_RECT();
                    WriteRegion.Left = (short)(left - 1);
                    WriteRegion.Top = (short)(top - 1);
                    WriteRegion.Right = (short)(right - 1);
                    WriteRegion.Bottom = (short)(bottom - 1);

                    NativeMethods.WriteConsoleOutput(_StdOutputHandle, buffer, BufferSize, BufferCoord, ref WriteRegion);
                }
                else
                {
                    if (buffer != null)
                    {
                        // TODO Restore from buffer
                    }
                }
            }
        }