winfrom窗体加载控制台程序,可以自定方输出语句颜色,如下图所示

怎么实现的此功能,网上有大把的方法,我这里已经把方法打包成了一个类,只需要引用调用就可以使用了,写的比较粗糙,如有发现需要改进的地方欢迎提出!

至于使用方法很简单,把这个类复制到解决方案中,在引用 LLog , 另外在 程序初始化下面加上 XLog.AllocConsole();  就可以在想使用的地方调用方法 XLog.Logx() 使用了,

如图所示:

注意:图片中显示 [/c15] xxxxxx[/c16] 必须成对出现才不会出现输出颜色覆盖下一行内容, 此方法效率略低,水平有限还没想出高效方法,如想某行只输出某种颜色必须在语句后面加上 [/c16] 结尾  如:[/c1] 我是内容 [/c16]

 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_Console(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);
             }
         }

         /// <summary>
         /// 传入字符跟指定的颜色标签
         /// </summary>
         /// <param name="str">[c1]蓝色 [c2]青色(蓝绿色) [c3]藏蓝色 [c4]深紫色(深蓝绿色) [c5]深灰色 [c6]深绿色 [c7]深紫红色 [c8]深红色 [c9]深黄色 [c10]灰色 [c11]绿色 [c12]紫红色 [c13]红色 [c14]白色 [c15]黄色 [c16]默认灰色</param>
         public static void Logx(string str)
         {
             XLog.Logx_Console(str + "\r\n");
         }
     }
 }

winfrom窗体加载控制台程序,可以自定义输出语句颜色的更多相关文章

  1. jQuery自动加载更多程序

    1.1.1 摘要 现在,我们经常使用的微博.微信或其他应用都有异步加载功能,简而言之,就是我们在刷微博或微信时,移动到界面的顶端或低端后程序通过异步的方式进行加载数据,这种方式加快了数据的加载速度,由 ...

  2. jQuery自动加载更多程序(转)

    jQuery自动加载更多程序   1.1.1 摘要 现在,我们经常使用的微博.微信或其他应用都有异步加载功能,简而言之,就是我们在刷微博或微信时,移动到界面的顶端或低端后程序通过异步的方式进行加载数据 ...

  3. 用UBOOT自带loadb命令加载应用程序到SDRAM中运行的方法

    S3C44B0开发板中,用UBOOT自带loadb命令加载应用程序到SDRAM中运行的方法    1.开发板说明:  开发板上已有移植好的UBOOT运行.   2.交叉编译工具链为arm-linu-g ...

  4. 用window的onload事件,窗体加载完毕的时候

    <script type="text/javascript"> //用window的onload事件,窗体加载完毕的时候 window.onload=function( ...

  5. chrome 安装setupvpn 解决chorme未能成功加载扩展程序的问题

    一: vpn文件    https://pan.baidu.com/s/1wZV2HAC3GHlh1bjlvbilRg 提取码:  gz72; 二 : 安装步骤 ------请看完以下步骤,不要直接拖 ...

  6. C#窗体加载和控件加载不同步导致控件闪烁

    窗体加载和控件加载不同步导致的控件闪烁现象:// 代码块加在父窗体中的任意位置,解决窗体加载和控件加载不同步导致的控件闪烁问题        protected override CreatePara ...

  7. spring-boot 如何加载rsources下面的自定义配置文件

    spring-boot 如何加载resources下面的自定义配置文件 https://segmentfault.com/q/1010000006828771?_ea=1144561

  8. DevExpress窗体加载等待

    using DevExpress.XtraEditors; using DevExpress.XtraSplashScreen; using System; using System.Collecti ...

  9. C#Winfrom中,窗体加载时会自动执行一次控件的textchange事件,怎么让它不执行?

    http://zhidao.baidu.com/link?url=iTSyfa5_RJBSb37S8efdWoL5eDMrnxeAm-prhGSNBXqdP9r7PzNDQTc7gVzJgCNdzli ...

随机推荐

  1. 对Java的初识

    什么是计算机程序:    为了让计算机执行某些操作或解决某个问题而编写的一系列有序指令的集合.(简单来说就是记算机为完成某些功能生产的一系列有序指令集合); Java的来历:  Java的初期开发早在 ...

  2. ngRepeat track by

    刚刚看见一篇文章讲述track by的功能的,大致记录如下: 1. ng-repeat="friend in friends" 一般不使用track by的情况下,每次刷新DOM, ...

  3. RG_4

    集训前半段马上就要结束了. 很多作业等待着我. 真希望作业君不喜欢我.

  4. sdk&jdk&jre

    1. jre and jdkJRE(Java Runtime Enviroment)是Java的运行环境.面向Java程序的使用者,而不是开发者.如果你仅下载并安装了JRE,那么你的系统只能运行Jav ...

  5. MySQL高级查询(二)

    EXISTS 和NOT EXISTS子查询 EXISTS子查询 语法:   SELECT ……… FROM 表名 WHERE EXISTS (子查询); 例: SELECT `studentNo` A ...

  6. JSP静态化(伪静态)

    关于URLRewirte的用法:该方法只是实现了url的伪静态化,并不是真正的静态化. URLRewirte版本:urlrewrite-2.6.0.jar URLRewirte的用处: 1.满足搜索引 ...

  7. 使用javaAPI操作hdfs

    欢迎到https://github.com/huabingood/everyDayLanguagePractise查看源码. 一.构建环境 在hadoop的安装包中的share目录中有hadoop所有 ...

  8. Perfect Pth Powers poj1730

    Perfect Pth Powers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16383   Accepted: 37 ...

  9. hdu1512 Monkey King(左偏树 + 并查集)

    Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o ...

  10. http://codeforces.com/contest/845

    A. Chess Tourney time limit per test 1 second memory limit per test 256 megabytes input standard inp ...