卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章1829本站已运行4109

C#控制台内容重定向到richTextBox中

/////////////////////////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////////////////////
        // 将控制台输出重定向到文本框 可过滤
        // 创建一个内存流来捕获控制台输出
        private MemoryStream consoleOutput = new MemoryStream();
        // 用于存储要过滤的关键字
        private string[] filterKeywords = { "当前字符数量", "最大字符数量", "当前字符数量百分比" };
        private void InitializeConsoleRedirection()
        {
            // 重定向控制台输出
            Console.SetOut(new StreamWriter(consoleOutput) { AutoFlush = true });
            // 设置一个定时器,定期检查控制台输出
            System.Timers.Timer timer = new System.Timers.Timer(1000);
            timer.Elapsed += (sender, e) => CheckConsoleOutput();
            timer.Start();
        }
        private void CheckConsoleOutput()
        {
            // 将流的位置重置到开始
            consoleOutput.Position = 0;
            // 读取流中的内容
            string output = new StreamReader(consoleOutput).ReadToEnd();
            // 如果有内容,则显示在RichTextBox中,并过滤关键字
            if (!string.IsNullOrEmpty(output))
            {
                string filteredOutput = FilterWholeLineContainingKeywords(output);
                // 由于richTextBox1的更新需要在UI线程中执行,所以使用Invoke方法
                richTextBox1.Invoke((MethodInvoker)(() =>
                {
                    richTextBox1.AppendText(filteredOutput);
                }));
            }
            // 清空流中的内容
            consoleOutput.SetLength(0);
        }
        //此方法过滤关键词整行
        private string FilterWholeLineContainingKeywords(string input)
        {
            //正则表达式来匹配包含关键词的行
            //此正则表达式不会删除/r/n换行 @"(?i)(?m)^(?=.*\b({0})\b).*"
            //此正则表达式会删除/r/n换行 @"(?im)^(?=.*\b({0})\b).*\r?\n?"
            string pattern = string.Format(@"(?im)^(?=.*\b({0})\b).*\r?\n?", string.Join("|", filterKeywords));
            //屏蔽包含关键词的字符串
            return Regex.Replace(input, pattern, string.Empty);
            //string replacement = "***"; // 用于替换整行的文本
            //返回值包含replacement替代的整行字符串
            //return Regex.Replace(input, pattern, replacement, RegexOptions.IgnoreCase);
        }
        //此方法仅过滤关键词
        private string FilterKeywords(string input)
        {
            // 使用正则表达式替换关键字
            foreach (string keyword in filterKeywords)
            {
                input = Regex.Replace(input, keyword, "***", RegexOptions.IgnoreCase);
            }
            return input;
        }
        //////////////////////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////////////////////////
         
 
        // 将控制台输出重定向到文本框 不可过滤
        private void DisplayMessageInRichTextBox1()
        {
            // 将控制台输出重定向到文本框
            RichTextBox richTextBox = this.richTextBox1;
            Console.SetOut(new richTextBoxStreamWriter(richTextBox));
        }
 
        //添加消息进入RichTextBox1中
        private void DisplayMessageInRichTextBox(string message)
        {
            if (richTextBox1.InvokeRequired)
            {
                richTextBox1.Invoke(new MethodInvoker(delegate
                {
                    richTextBox1.AppendText(message + Environment.NewLine);
                }));
            }
            else
            {
                richTextBox1.AppendText(message + Environment.NewLine);
            }
        }
 
        //当richTextBox1内容被改变时
        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            // 获取RichTextBox1的最大字符数量
            toolStripStatusLabel3.Text = richTextBox1.MaxLength.ToString();
 
            // 获取RichTextBox1的当前字符数量
            toolStripStatusLabel5.Text = richTextBox1.Text.Length.ToString();
 
            // 这里可以继续使用lengths数组
            // 计算当前字符数量占最大字符数量的百分比
            double percentage = Math.Round((double)richTextBox1.Text.Length / richTextBox1.MaxLength * 100, 2); 
 
            // 检查是否达到最大值的70%
            if (percentage >= 70 && percentage <= 90)
            {
                // 字体颜色变为橘黄色
                toolStripStatusLabel4.ForeColor = Color.Orange;
                toolStripStatusLabel5.ForeColor = Color.Orange;
            }
            // 检查是否达到最大值的90%
            else if (percentage >= 90)
            {
                // 字体颜色变为红色
                toolStripStatusLabel4.ForeColor = Color.Red;
                toolStripStatusLabel5.ForeColor = Color.Red;
            }
            // 否则,字体颜色恢复为默认颜色
            else
            {
                toolStripStatusLabel4.ForeColor = Color.Green;
                toolStripStatusLabel5.ForeColor = Color.Green;
            }
 
            // 输出到控制台(或显示在文本框中)
            Console.WriteLine("当前字符数量: " + richTextBox1.Text.Length);
            Console.WriteLine("最大字符数量: " + richTextBox1.MaxLength);
            Console.WriteLine("当前字符数量百分比: " + percentage);
        }
 
卓越飞翔博客
上一篇: Python 发票pdf文件解析
下一篇: C&C++ 修改x64dbg插件 Multiline Ultimate Assembler 完美支持中文
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏