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. JavaScript中的alert、confirm、prompt

    alert: var a=alert('Alert');//界面只有一個確定alert(a);   //返回值為undefined confirm: var c= confirm('Confirm') ...

  2. python 库之lxml安装 坑一个

    error: command 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python ...

  3. jenkins~集群分发功能的具体实现

    前一讲主要说了jenkins分发的好处<jenkins~集群分发功能和职责处理>,它可以让具体的节点干自己具体的事,比如windows环境下的节点,它只负责编译,发布windows的生态环 ...

  4. Glide 这样用,更省内存!!!

    一.前言 Glide 是 Google 官方推荐的一款图片加载库,使用起来也非常的简单便利,Glide 它帮我们完成了很多很重要,但是却通用的功能,例如:图片的加载压缩.展示.加载图片的内存管理等等. ...

  5. Android Studio安装应用时报错 installation failed with message Failed to finalize session......

    解决方法: 在AndroidManifest.xml中的provider中的authorities后加几个数字即可. 2017.09.01: 我发现有的项目AndroidManifest.xml中没有 ...

  6. leetCode没那么难啦 in Java (二)

    介绍    本篇介绍的是标记元素的使用,很多需要找到正确元素都可以将正确元素应该插入的位置单独放置一个标记来记录,这样可以达到原地排序的效果. Start 27.RemoveElement 删除指定元 ...

  7. 入门VMware Workstation下的Debian学习之基本命令(二)

    本章记录如何在Linux终端进行命令操作命令下载路径,模拟终端.dkpg管理软件包.用户组和用户管理.文件属性.文件与目录管理.查看磁盘使用量. (1)命令下载路径: wegt 路径; (2)模拟终端 ...

  8. FPGA IN 金融领域

    何为金融: 金融指货币的发行.流通和回笼,贷款的发放和收回,存款的存入和提取,汇兑的往来等经济活动.金融(FIN)就是对现有资源进行重新整合之后,实现价值和利润的等效流通. 金融主要包括银行.证券.基 ...

  9. Python实战之文件操作的详细简单练习

    ['_CHUNK_SIZE', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__' ...

  10. Python系列之模块、和字符串格式化

    Python 模块 模块让你能够有逻辑地组织你的Python代码段. 把相关的代码分配到一个 模块里能让你的代码更好用,更易懂. 模块也是Python对象,具有随机的名字属性用来绑定或引用. 模块分为 ...