API改变控制台的颜色

class Class1
 {
  static void Main(string[] args)
  {
   Class1 c =new Class1();
   c.change();
  }
  [DllImport("kernel32.dll", SetLastError=true)]
  public static extern bool SetConsoleTextAttribute(
   IntPtr hConsoleOutput,
   CharacterAttributes wAttributes);
  /* declaring the setconsoletextattribute function*/
  [DllImport("kernel32.dll")]
  public static extern IntPtr GetStdHandle(int nStdHandle);
  /*declaring the getstdhandle funtion to get thehandle
    that would be used in setConsoletextattribute function */
  void change()
  {
   IntPtr hOut; /* declaring variable to get handle*/
   hOut= GetStdHandle(-11);/* -11 is sent for output device*/
   /*Displaying text in different colors and background colors*/
   SetConsoleTextAttribute(hOut, CharacterAttributes.FOREGROUND_BLUE );
   Console.WriteLine(" Subhan ALLAH ");
   SetConsoleTextAttribute(hOut, CharacterAttributes.BACKGROUND_RED);
   Console.WriteLine(" Alkhamdolillah ");
   SetConsoleTextAttribute(hOut, CharacterAttributes.BACKGROUND_GREEN );
   Console.WriteLine(" Allah O Akbar ");
   SetConsoleTextAttribute(hOut, CharacterAttributes.FOREGROUND_RED );
   Console.WriteLine(" Pakistan ");
   Console.ReadLine();
  }
  /* This enumeration lists all of the character attributes.
     You can combine attributes to achieve specific effects.*/
  public enum CharacterAttributes
  {
   FOREGROUND_BLUE = 0x0001,
   FOREGROUND_GREEN = 0x0002,
   FOREGROUND_RED = 0x0004,
   FOREGROUND_INTENSITY = 0x0008,
   BACKGROUND_BLUE = 0x0010,
   BACKGROUND_GREEN = 0x0020,
   BACKGROUND_RED = 0x0040,
   BACKGROUND_INTENSITY = 0x0080,
   COMMON_LVB_LEADING_BYTE = 0x0100,
   COMMON_LVB_TRAILING_BYTE = 0x0200,
   COMMON_LVB_GRID_HORIZONTAL = 0x0400,
   COMMON_LVB_GRID_LVERTICAL = 0x0800,
   COMMON_LVB_GRID_RVERTICAL = 0x1000,
   COMMON_LVB_REVERSE_VIDEO = 0x4000,
   COMMON_LVB_UNDERSCORE = 0x8000
  }
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值