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. mybatis-主配置文件介绍

    mybatis下载地址:http://code.google.com/p/mybatis/ 学习手册地址:http://mybatis.github.io/mybatis-3/zh/index.htm ...

  2. 在Myeclipse中用Java语言操作mysql数据库

    package OperateMysql; import java.sql.*; public class MysqlTest { public static void main(String[] a ...

  3. Apache Spark 2.2.0 中文文档 - Spark Streaming 编程指南 | ApacheCN

    Spark Streaming 编程指南 概述 一个入门示例 基础概念 依赖 初始化 StreamingContext Discretized Streams (DStreams)(离散化流) Inp ...

  4. mongodb 与关系型数据库

    设计 MongDB 模式时应注意的问题 根据用户需求来设计模式. 如果想一起使用对象,请将这些对象合并到一个文档中,否则要将它们分开(但是要确保不需要连接). 经常复制数据(但要有一定限度),因为与计 ...

  5. WPA/WPA2加密破解

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

  6. MySQL高级查询(二)

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

  7. 理解及操作环境变量(基于Mac操作)

    通过本文,简单的了解下环境变量及其操作,与便于遇到相关问题时能够准确快捷的解决. 什么是环境变量 An environment variable is a dynamic-named value th ...

  8. Writing Science 笔记 6.20

    1.写作的六个要素:S: Simple 简单的 U: Unexpected 出人意料的 C: Concrete 具体的  C: Credible 可信的  E: Emotional S: Storie ...

  9. Linux入门之常用命令(8)上传下载

    [什么是rz/sz (lsz/lrz)]  简单说就是,可以很方便地用这两个sz/rz工具,实现Linux下和Windows之间的文件传输(发送和接收),速度大概为10KB/s,适合中小文件.rz/s ...

  10. Java 中的语法糖

    百度百科对语法糖的定义 语法糖(Syntactic sugar),也译为糖衣语法,是由英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语,指计算机语言中添加的某种语法,这 ...