http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 3942|回复: 5

[其他] winform加载控制台程序怎么让指定的文字变颜色

[复制链接]
发表于 2017-5-23 23:34:36 | 显示全部楼层 |阅读模式
如果题:winform加载控制台程序怎么让指定的文字变颜色

现在用的是常见的 AllocConsole()和FreeConsole() 这种方式会让整行的颜色变成一样


要达如下图这样的效果要该怎么做呢?  如:图 Error 跟后面的整行文字是条完整的句子,  我只想让其中的error 或者 log 改变颜色 而不是整行改变颜色


是winform 程序中加载的 控制台日志输出,该怎么实现呢??? 求解。。。


QQ截图20170523232931.png


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2017-5-24 08:31:10 | 显示全部楼层
https://msdn.microsoft.com/zh-cn ... ole.backgroundcolor

[C#] 纯文本查看 复制代码
private static string Str = @"abckdidkdifskflsfsfs";
 
        static void Main(string[] args)
        {
 
            foreach(char c in Str)
            {
                if (c == 'd')
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.White;
                }
                Console.Write(c);
 
            }
            Console.WriteLine();
 
            Console.ReadKey();
 楼主| 发表于 2017-5-24 13:55:19 | 显示全部楼层
站长苏飞 发表于 2017-5-24 08:31
https://msdn.microsoft.com/zh-cn ... ole.backgroundcolor

[mw_shl_code=csharp,true]private static  ...

非常感谢,但是这种方式 会连自身也输出 如上 c==d 会连着 d 一起输出无法替换,
能不能以这种形式显示呢 比如:  这么一串字符

加载[c1]控制台[/c1]程序怎么让指定的[c2]文字[/c2]变颜色

让[c1][/c1] 中间的字符成红色 [c2][/c2]中间的成黄色,
标签成对出现缺一不可,其它中间部分未出现标签即为默认颜色 -----略笨,尝试了几种方式都没成功
发表于 2017-5-24 18:15:05 | 显示全部楼层
  没 有这个规范的语法,这个除非自己自定义一套
 楼主| 发表于 2017-5-24 22:42:25 | 显示全部楼层
代码实现了,就是有弊端, 输出 要是不加 \r\n 字符就会全在一行

[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace LLog
{
    /// <summary>  
    /// 与控制台交互  
    /// </summary>  
    static class XLog
    {

        [DllImport("kernel32.dll")]
        public static extern bool AllocConsole();
        [DllImport("kernel32.dll")]
        public static extern bool FreeConsole();

        /// <summary>
        /// 修改控制台标题
        /// </summary>
        public static string Title
        {
            get
            {
                string result;
                try
                {
                    result = Console.Title;
                }
                catch
                {
                    result = "";
                }
                return result;
            }
            set
            {
                Console.Title = value;
            }
        }

        /// <summary>
        /// 调用输出日志方法从c1-c15 共15种颜色
        /// </summary>
        /// <param name="code">[c1]蓝色 [c2]青色(蓝绿色) [c3]藏蓝色 [c4]深紫色(深蓝绿色) [c5]深灰色 [c6]深绿色 [c7]深紫红色 [c8]深红色 [c9]深黄色 [c10]灰色 [c11]绿色 [c12]紫红色 [c13]红色 [c14]白色 [c15]黄色 [c16]默认灰色</param>
        public static void Logx(string code)
        {
            //①②③④⑤⑥⑦⑧⑨⑩⑾⑿⒀⒁⒂
            string str = code;
            string output = str.Replace("[/c1]", "①").Replace("[/c2]", "②").Replace("[/c3]", "③").Replace("[/c4]", "④").Replace("[/c5]", "⑤").Replace("[/c6]", "⑥").Replace("[/c7]", "⑦").Replace("[/c8]", "⑧").Replace("[/c9]", "⑨").Replace("[/c10]", "⑩").Replace("[/c11]", "⑾").Replace("[/c12]", "⑿").Replace("[/c13]", "⒀").Replace("[/c14]", "⒁").Replace("[/c15]", "⒂").Replace("[/c16]","⒃");
            string display = "";
            foreach (char c in output)
            {
                if (c == '①')
                {
                    Console.ForegroundColor = ConsoleColor.Blue;
                }

                if (c == '②')
                {
                    Console.ForegroundColor = ConsoleColor.Cyan;
                }

                if (c == '③')
                {
                    Console.ForegroundColor = ConsoleColor.DarkBlue;
                }
                if (c == '④')
                {
                    Console.ForegroundColor = ConsoleColor.DarkCyan;
                }

                if (c == '⑤')
                {
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                }

                if (c == '⑥')
                {
                    Console.ForegroundColor = ConsoleColor.DarkGreen;
                }

                if (c == '⑦')
                {
                    Console.ForegroundColor = ConsoleColor.DarkMagenta;
                }

                if (c == '⑧')
                {
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                }

                if (c == '⑨')
                {
                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                }

                if (c == '⑩')
                {
                    Console.ForegroundColor = ConsoleColor.Gray;
                }

                if (c == '⑾')
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                }

                if (c == '⑿')
                {
                    Console.ForegroundColor = ConsoleColor.Magenta;
                }

                if (c == '⒀')
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                }

                if (c == '⒁')
                {
                    Console.ForegroundColor = ConsoleColor.White;
                }

                if (c == '⒂')
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                }

                if (c == '⒃') 
                {
                    Console.ForegroundColor = ConsoleColor.Gray;
                }

                display = c.ToString();
                display = display.Replace("①", "").Replace("②", "").Replace("③", "").Replace("④", "").Replace("⑤", "").Replace("⑥", "").Replace("⑦", "").Replace("⑧", "").Replace("⑨", "").Replace("⑩", "").Replace("⑾", "").Replace("⑿", "").Replace("⒀", "").Replace("⒁", "").Replace("⒂", "").Replace("⒃", "");
                Console.Write(display);
            }
        }
    }  
}
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-20 17:37

© 2014-2021

快速回复 返回顶部 返回列表