Console中如何输出彩色字体?

lovesharp32 2004-05-03 01:24:02
Console中如何输出彩色字体?
...全文
64 1 点赞 打赏 收藏 举报
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
wolftop 2004-05-03
先是consolecolour.cs类,用来调用颜色
// ConsoleColour.cs
using System;

// need this to make API calls
using System.Runtime.InteropServices;

namespace PFitzsimons.ConsoleColour
{
  /// <summary>
  /// Static class for console colour manipulation.
  /// </summary>
  public class ConsoleColour
  {
    // constants for console streams
    const int STD_INPUT_HANDLE = -10;
    const int STD_OUTPUT_HANDLE = -11;
    const int STD_ERROR_HANDLE = -12;

    [DllImportAttribute("Kernel32.dll")]
    private static extern IntPtr GetStdHandle
    (
      int nStdHandle // input, output, or error device
    );

    [DllImportAttribute("Kernel32.dll")]
    private static extern bool SetConsoleTextAttribute
    (
      IntPtr hConsoleOutput, // handle to screen buffer
      int wAttributes  // text and background colors
    );

    // colours that can be set
    [Flags]
    public enum ForeGroundColour
    {
      Black = 0x0000,
      Blue = 0x0001,
      Green = 0x0002,
      Cyan = 0x0003,
      Red = 0x0004,
      Magenta = 0x0005,
      Yellow = 0x0006,
      Grey = 0x0007,
      White = 0x0008
    }

    // class can not be created, so we can set colours
    // without a variable
    private ConsoleColour()
    {
    }

    public static bool SetForeGroundColour()
    {
      // default to a white-grey
      return SetForeGroundColour(ForeGroundColour.Grey);
    }

    public static bool SetForeGroundColour(
      ForeGroundColour foreGroundColour)
    {
      // default to a bright white-grey
      return SetForeGroundColour(foreGroundColour, true);
    }

    public static bool SetForeGroundColour(
      ForeGroundColour foreGroundColour,
      bool brightColours)
    {
      // get the current console handle
      IntPtr nConsole = GetStdHandle(STD_OUTPUT_HANDLE);
      int colourMap;
      
      // if we want bright colours OR it with white
      if (brightColours)
        colourMap = (int) foreGroundColour |
          (int) ForeGroundColour.White;
      else
        colourMap = (int) foreGroundColour;

      // call the api and return the result
      return SetConsoleTextAttribute(nConsole, colourMap);
    }
  }
}

然后是主程序
// Console.cs
using System;

// we want color output
using PFitzsimons.ConsoleColour;

namespace MyApp
{
  public class MyApp
  {
    public static void Main()
    {
       ConsoleColour.SetForeGroundColour(
        ConsoleColour.ForeGroundColour.Green);

      Console.WriteLine("Text in green");

      ConsoleColour.SetForeGroundColour(
        ConsoleColour.ForeGroundColour.Red);

      Console.WriteLine("Text in red");

      // reset console back to a default
      ConsoleColour.SetForeGroundColour(ConsoleColour.ForeGroundColour.Yellow,true);
   //ConsoleColour.SetForeGroundColour();
   //ConsoleColour.SetForeGroundColour(ConsoleColour.ForeGroundColour.Yellow,false);

    }
  }
}


分别存成ConsoleColour.cs和Console.cs,然后提示符下编译
CSC /t:library ConsoleColour.cs
CSC /r:ConsoleColour.dll Console.cs
生成console.exe可执行文件
  • 打赏
  • 举报
回复
相关推荐
发帖
C#
加入

10.6w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2004-05-03 01:24
社区公告

让您成为最强悍的C#开发者