⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tty.cs

📁 Data Structures and Algorithms with Object-Oriented Design Patterns in C# 这本书的范例代码dll自己反编译的source
💻 CS
字号:
namespace Opus6
{
    using System;
    using System.Drawing;
    using System.IO;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;

    public abstract class TTY : UserControl
    {
        static TTY()
        {
            TTY.reader = System.Console.In;
            TTY.writer = System.Console.Out;
        }

        public TTY()
        {
            this.outputArea = new TextBox();
            this.outputArea.Location = new System.Drawing.Point(0, 0);
            this.outputArea.Size = new Size(480, 320);
            this.outputArea.Multiline = true;
            this.outputArea.WordWrap = true;
            this.outputArea.ScrollBars = ScrollBars.Vertical;
            this.outputArea.ReadOnly = true;
            base.Controls.Add(this.outputArea);
            this.inputBox = new TextBox();
            this.inputBox.Location = new System.Drawing.Point(0, 320);
            this.inputBox.Size = new Size(480, 20);
            this.inputBox.MaxLength = 80;
            base.Controls.Add(this.inputBox);
            this.Dock = DockStyle.Fill;
            TTY.writer = new TTYWriter(this);
            TTY.reader = new TTYReader(this);
            new Thread(new ThreadStart(this.Body)).Start();
        }

        private void Body()
        {
            try
            {
                Thread.Sleep(0x3e8);
                this.Run();
                this.inputBox.Enabled = false;
            }
            catch (Exception exception1)
            {
                TTY.writer.WriteLine("Caught: " + exception1);
                return;
            }
            finally
            {
                this.inputBox.Enabled = false;
            }
        }

        public abstract void Run();

        public static TextReader In
        {
            get
            {
                return TTY.reader;
            }
        }

        public static TextWriter Out
        {
            get
            {
                return TTY.writer;
            }
        }


        private TextBox inputBox;
        private TextBox outputArea;
        private static TextReader reader;
        private static TextWriter writer;


        private class TTYReader : TextReader
        {
            public TTYReader(TTY tty)
            {
                this.endOfFile = false;
                this.tty = tty;
                this.buffer = new StringBuilder();
                tty.inputBox.KeyPress += new KeyPressEventHandler(this.KeyPress);
            }

            public void KeyPress(object sender, KeyPressEventArgs args)
            {
                StringBuilder builder1;
                char ch1 = args.KeyChar;
                if (args.KeyChar == '\r')
                {
                    string text1 = this.tty.inputBox.Text + "\r\n";
                    this.tty.outputArea.AppendText(text1);
                    this.tty.inputBox.Clear();
                    args.Handled = true;
                    lock ((builder1 = this.buffer))
                    {
                        this.buffer.Append(text1);
                        Monitor.Pulse(this.buffer);
                    }
                }
                else if (args.KeyChar == '\x0004')
                {
                    string text2 = this.tty.inputBox.Text + "^D";
                    text2 = text2 + "\r\n";
                    this.tty.outputArea.AppendText(text2);
                    this.tty.inputBox.Clear();
                    this.tty.inputBox.Enabled = false;
                    args.Handled = true;
                    lock ((builder1 = this.buffer))
                    {
                        this.buffer.Append(text2);
                        this.endOfFile = true;
                        Monitor.Pulse(this.buffer);
                    }
                }
            }

            public override int Read()
            {
                int num1;
                lock (this.buffer)
                {
                    while ((this.buffer.Length == 0) && !this.endOfFile)
                    {
                        Monitor.Wait(this.buffer);
                    }
                    if (this.endOfFile)
                    {
                        return -1;
                    }
                    num1 = this.buffer[0];
                    this.buffer.Remove(0, 1);
                }
                return num1;
            }

            public override int Read(char[] buffer, int index, int count)
            {
                return 0;
            }


            private StringBuilder buffer;
            private bool endOfFile;
            private TTY tty;
        }

        private class TTYWriter : TextWriter
        {
            public TTYWriter(TTY tty)
            {
                this.tty = tty;
            }

            public override void Write(char value)
            {
                this.tty.outputArea.AppendText(value.ToString());
            }

            public override void Write(string value)
            {
                this.tty.outputArea.AppendText(value);
            }

            public override void Write(char[] buffer, int index, int count)
            {
                this.tty.outputArea.AppendText(new string(buffer, index, count));
            }


            public override System.Text.Encoding Encoding
            {
                get
                {
                    return new ASCIIEncoding();
                }
            }


            private TTY tty;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -