zoukankan      html  css  js  c++  java
  • C# 控制台应用程序输出颜色字体[更正版]

    首先感谢院子里的“yanxinchen”,之前的方法是通过c#调用系统api实现的,相比之下我的有点画蛇添足了,哈哈。

    最佳解决方案的代码:

    static void Main(string[] args)
    {
    Console.ForegroundColor = ConsoleColor.Green;
    Console.WriteLine("Hello, color text!");
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine("Hello, color text!");
    Console.ReadKey();
    }

    效果如图:

    =================================================================================

    我之前的代码如下(已过时):

    控制台应用程序字体本身不能输出带颜色的字体,所有就需要调用系统的api实现,下面是一个封装好的类,另赋调用的方法,使用起来很方便,效果如图:

    封装调用系统api的类ConsoleColor.cs代码如下:

    ConsoleColor.cs
    using System.Runtime.InteropServices;

    namespace Test
    {
    /// Summary description for ConsoleColor.
    public class ConsoleColor
    {
    private int hConsoleHandle;
    private COORD ConsoleOutputLocation;
    private CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo;
    private int OriginalColors;

    private const int STD_OUTPUT_HANDLE = -11;

    [DllImport("kernel32.dll", EntryPoint = "GetStdHandle", SetLastError = true,
    CharSet = CharSet.Auto,
    CallingConvention = CallingConvention.StdCall)]
    private static extern int GetStdHandle(int nStdHandle);

    [DllImport("kernel32.dll", EntryPoint = "GetConsoleScreenBufferInfo",
    SetLastError = true, CharSet = CharSet.Auto,
    CallingConvention = CallingConvention.StdCall)]
    private static extern int GetConsoleScreenBufferInfo(int hConsoleOutput,
    ref CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);

    [DllImport("kernel32.dll", EntryPoint = "SetConsoleTextAttribute",
    SetLastError = true, CharSet = CharSet.Auto,
    CallingConvention = CallingConvention.StdCall)]
    private static extern int SetConsoleTextAttribute(int hConsoleOutput,
    int wAttributes);

    public enum Foreground
    {
    Blue = 0x00000001,
    Green = 0x00000002,
    Red = 0x00000004,
    Intensity = 0x00000008
    }

    public enum Background
    {
    Blue = 0x00000010,
    Green = 0x00000020,
    Red = 0x00000040,
    Intensity = 0x00000080
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct COORD
    {
    short X;
    short Y;
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct SMALL_RECT
    {
    short Left;
    short Top;
    short Right;
    short Bottom;
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct CONSOLE_SCREEN_BUFFER_INFO
    {
    public COORD dwSize;
    public COORD dwCursorPosition;
    public int wAttributes;
    public SMALL_RECT srWindow;
    public COORD dwMaximumWindowSize;
    }

    // Constructor.
    public ConsoleColor()
    {
    ConsoleInfo = new CONSOLE_SCREEN_BUFFER_INFO();
    ConsoleOutputLocation = new COORD();
    hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsoleHandle, ref ConsoleInfo);
    OriginalColors = ConsoleInfo.wAttributes;
    }

    public void TextColor(int color)
    {
    SetConsoleTextAttribute(hConsoleHandle, color);
    }

    public void ResetColor()
    {
    SetConsoleTextAttribute(hConsoleHandle, OriginalColors);
    }
    }
    }

    在Main方法的调用代码如下:

    ConsoleColor TextChange = new ConsoleColor();
    Console.WriteLine("Original Colors");
    Console.WriteLine("Press Enter to Begin");
    Console.ReadLine();
    TextChange.TextColor((int)ConsoleColor.Foreground.Green +
    (int)ConsoleColor.Foreground.Intensity);
    Console.WriteLine("THIS TEXT IS GREEN");
    Console.WriteLine("Press Enter to change colors again");
    Console.ReadLine();
    TextChange.TextColor((int)ConsoleColor.Foreground.Red +
    (int)ConsoleColor.Foreground.Blue +
    (int)ConsoleColor.Foreground.Intensity);
    Console.WriteLine("NOW THE TEXT IS PURPLE");
    Console.WriteLine("Press Enter to change colors again");
    Console.ReadLine();
    TextChange.TextColor((int)ConsoleColor.Foreground.Blue +
    (int)ConsoleColor.Foreground.Intensity +
    (int)ConsoleColor.Background.Green +
    (int)ConsoleColor.Background.Intensity);
    Console.WriteLine("NOW THE TEXT IS BLUE AND BACKGROUND OF IT IS GREEN");
    Console.WriteLine("Press Enter change everything back to normal");
    Console.ReadLine();
    TextChange.ResetColor();
    Console.WriteLine("Back to Original Colors");
    Console.WriteLine("Press Enter to Terminate");
    Console.ReadLine();




    关注下面二维码,订阅更多精彩内容。
    微信打赏
    关注公众号(加好友):

  • 相关阅读:
    案例------存储过程
    案例------冒泡排序
    案例------递归调用
    天气预报接口api(中国天气网)
    【转】ubuntu64,ndk-r9 编译 ffmpeg 2.1.1的config文件
    android权限大全
    JNI 回调小记
    java设置环境变量小工具
    bootstrap之双日历时间段选择控件—daterangepicker(汉化版)
    一个 bootstrap 弹出框插件
  • 原文地址:https://www.cnblogs.com/vipstone/p/2193426.html
Copyright © 2011-2022 走看看