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. temp-黄河农商行路径

    -------------------------------黄河农村商业银行------------------------------------ --1.--svn 地址:http://10.0 ...

  2. JDK8-废弃永久代(PermGen)迎来元空间(Metaspace)

    1.背景 2.为什么废弃永久代(PermGen) 3.深入理解元空间(Metaspace) 4.总结 ========正文分割线===== 一.背景 1.1 永久代(PermGen)在哪里? 根据,h ...

  3. 一张图告诉你移动Web前端所有技术(工程化、预编译、自动化)

    你要的移动web前端都在这里! 大前端方向:移动Web前端.Native客户端.Node.js. 大前端框架:React.Vue.js.Koa 跨终端技术:HTML5.CSS 3.JavaScript ...

  4. WPA/WPA2加密破解

    WPA/WPA2无线密码破解这里主要介绍两种方法:穷举PIN码.穷举密码 穷举PIN码(共11000种组合)破解速度快,需要一些条件(路由器支持WPS.QSS功能),路由器信号良好.无论密码多复杂,条 ...

  5. MySql join on 和 where

    原文:http://www.cnblogs.com/Jessy/p/3525419.html left join :左连接,返回左表中所有的记录以及右表中连接字段相等的记录. right join : ...

  6. 程序员的自我修养九Windows下的动态链接

    9.1 DLL简介 DLL即动态链接库的缩写,它相对于Linux下的共享对象. Windows下的DLL文件和EXE文件实际上是一个概念,它们都是有PE格式的二进制文件. 微软希望通过DLL机制加强软 ...

  7. 【JAVA零基础入门系列】Day4 变量与常量

    这一篇主要讲解Java中的变量,什么是变量,变量的作用以及如何声明,使用变量. 那么什么是变量?对于初学者而言,可以将变量理解为盒子,这些盒子可以用来存放数据,不同类型的数据需要放在对应类型的盒子里. ...

  8. 【NOIP】OpenJudge - 15:银行利息

    #include<stdio.h>//银行利息 int main() { float a,b; int i,c,d; scanf("%f%f%d",&a,&am ...

  9. apache 安装/mod_dir.so: undefined symbol: apr_array_clear

    apache 安装好后 启动出错: httpd: Syntax error on line 143 of /usr/local/apache2/conf/httpd.conf: Cannot load ...

  10. Java历程-初学篇 Day05选择结构(2)

    一,switch 由于本作者学的是jdk6.0版本,我知道7.0可以使用字符串,但是我就不改了 语法: switch(char类型/int类型){ case 值: //输出 break; ... de ...