CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

Console中如何输出彩色字体?

楼主lovesharp32(kkk)2004-05-03 01:24:02 在 .NET技术 / C# 提问

Console中如何输出彩色字体? 问题点数:30、回复次数:1Top

1 楼lijun84(李俊俊)回复于 2004-05-03 01:26:31 得分 30

先是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可执行文件    
  Top

相关问题

  • 输出文字字体的问题
  • 如何输出彩色字符?
  • TextOut输出文本时,如何使字体背景为透明
  • 如何把windows的字体输出到一个位图?
  • 如何改变pDC->TextOut()输出字体的大小?
  • 急!急!急!在bmp图象上输出字体的问题
  • 在VC中怎么改变输出字体的大小
  • 如何设定<%="hello word!%>输出的字体及颜色?
  • 请问有没有一个字体输出的函数?
  • 请问如何设置输出文本的字体?

关键词

  • consolecolour
  • console
  • handle
  • const int std
  • static

得分解答快速导航

  • 帖主:lovesharp32
  • lijun84

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo