控制台程序中控制输出颜色

来源:互联网 发布:生活服务淘宝店铺 编辑:程序博客网 时间:2024/04/30 00:27

    public class color
    {
        [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);
        public enum CharacterAttributes
        {
            aa = 0x0007,
            FOREGROUND_BLUE = 0x0001,
            FOREGROUND_GREEN = 0x0002,
            ground_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
        }
    } 

 

调用:

声明: IntPtr hOut = color.GetStdHandle(-11);

输出时:color. SetConsoleTextAttribute(hOut,color. CharacterAttributes.FOREGROUND_INTENSITY);

原创粉丝点击