改变控制台程序输出字体颜色

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Putting_color
{
    class ConsoleColour
    {
        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);

        [DllImportAttribute("Kernel32.dll")]
        private static extern bool SetConsoleTextAttribute(IntPtr hConsoleOutput, int wAttributes);

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

        //private ConsoleColor()
        //{}

        public static bool SetForeGroundColour()
        {
            return SetForeGroundColour(ForeGroundColour.Grey);
        }

        public static bool SetForeGroundColour(ForeGroundColour foreGroundColour)
        {
            return SetForeGroundColour(foreGroundColour,true);
        }

        public static bool SetForeGroundColour(ForeGroundColour foreGroundColour,bool brightColours)
        {
            IntPtr nConsole=GetStdHandle(STD_OUTPUT_HANDLE);
            int colourMap;

            if(brightColours)
            {
                colourMap=(int)foreGroundColour |(int)ForeGroundColour.White;
            }
            else
            {
                colourMap=(int)foreGroundColour;
            }

            return SetConsoleTextAttribute(nConsole,colourMap);
        }


    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace Putting_color
{
    class Program
    {
        static void Main(string[] args)
        {
            ConsoleColour.SetForeGroundColour(ConsoleColour.ForeGroundColour.Green);

            Console.Write("Test1.");
            for (int i = 0; i < 10; i++)
            {
                System.Threading.Thread.Sleep(500);
                Console.Write(".");
            }

            Console.WriteLine();

            ConsoleColour.SetForeGroundColour(ConsoleColour.ForeGroundColour.Red);
            Console.Write("Test2.");
            for (int i = 0; i < 10; i++)
            {
                System.Threading.Thread.Sleep(500);
                Console.Write(".");
            }
            ConsoleColour.SetForeGroundColour();

            Console.WriteLine();
            Console.ReadLine();
        }
    }
}

posted @ 2007-12-03 09:54  peak  阅读(615)  评论(0)    收藏  举报