如何使用 Visual C# 更改控制台窗口中的文字的背景颜色和前景颜色

转帖地址:http://support.microsoft.com/kb/319883/zh-cn

 

若要将控制台窗口将显示的文本的前景色和背景颜色使用 SetConsoleTextAttribute Win32 应用程序编程接口 (API) 函数。此函数设置屏幕缓冲区写入的字符的属性。

当您在运行时更改这些属性时,所做的更改是有效的只要控制台窗口处于打开状态。如果关闭,然后重新打开控制台窗口属性重置为它们的默认值。如果从已在运行的控制台窗口中的命令行执行的程序,文本属性所做的更改可用于为该控制台窗口,只要窗口打开时,即使您的程序将退出。因此,程序应在退出该程序前还原原始的颜色属性。

通过使用 GetConsoleScreenBufferInfo API 函数,您可以获得控制台窗口的文本属性。此函数填充 CONSOLE_SCREEN_BUFFER_INFO 结构包含有关当前输出缓冲区设置的信息的实例。此结构的 wAttribute 参数包含定义文本的前景色和背景颜色的颜色信息。可能的颜色是通过将红色、 绿色和蓝色的组合,可以创建任意颜色组合。

   OriginalColors = ConsoleInfo.wAttributes;
SetConsoleTextAttribute(hConsoleHandle, color);

使用 ResetColor 方法可以重置为原始值在程序开始执行时捕获的控制台窗口的输出缓冲区属性。

   SetConsoleTextAttribute(hConsoleHandle, OriginalColors);

分步示例

 

  1. 在 Visual Studio.net 或 Visual Studio 2005 中创建一个新的 Visual C# 控制台应用程序项目。
  2. 在解决方案资源管理器中,用鼠标右键单击您的项目,单击 添加 ,然后选择 添加类 将一个新类添加到您的程序。
  3. 粘贴下面的代码示例在类中创建的。验证的代码示例将替换所有的现有代码中 class.
       using System;
    using System.Runtime.InteropServices;

    namespace ConsoleColor
    {
    /// Summary description for Class2.
    public class Class2
    {
    private int hConsoleHandle;
    private COORD ConsoleOutputLocation;
    private CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo;
    private int OriginalColors;

    private const int STD_OUTPUT_HANDLE = -11;

    [DllImport("kernel32.dll", EntryPoint="GetStdHandle", SetLastError=true,
    CharSet=CharSet.Auto,
    CallingConvention=CallingConvention.StdCall)]
    private static extern int GetStdHandle(int nStdHandle);

    [DllImport("kernel32.dll", EntryPoint="GetConsoleScreenBufferInfo",
    SetLastError=true, CharSet=CharSet.Auto,
    CallingConvention=CallingConvention.StdCall)]
    private static extern int GetConsoleScreenBufferInfo(int hConsoleOutput,
    ref CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);

    [DllImport("kernel32.dll", EntryPoint="SetConsoleTextAttribute",
    SetLastError=true, CharSet=CharSet.Auto,
    CallingConvention=CallingConvention.StdCall)]
    private static extern int SetConsoleTextAttribute(int hConsoleOutput,
    int wAttributes);

    public enum Foreground
    {
    Blue = 0x00000001,
    Green = 0x00000002,
    Red = 0x00000004,
    Intensity = 0x00000008
    }

    public enum Background
    {
    Blue = 0x00000010,
    Green = 0x00000020,
    Red = 0x00000040,
    Intensity = 0x00000080
    }

    [StructLayout(LayoutKind.Sequential)] private struct COORD
    {
    short X;
    short Y;
    }

    [StructLayout(LayoutKind.Sequential)] private struct SMALL_RECT
    {
    short Left;
    short Top;
    short Right;
    short Bottom;
    }

    [StructLayout(LayoutKind.Sequential)] private struct CONSOLE_SCREEN_BUFFER_INFO
    {
    public COORD dwSize;
    public COORD dwCursorPosition;
    public int wAttributes;
    public SMALL_RECT srWindow;
    public COORD dwMaximumWindowSize;
    }

    // Constructor.
    public Class2()
    {
    ConsoleInfo = new CONSOLE_SCREEN_BUFFER_INFO();
    ConsoleOutputLocation = new COORD();
    hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsoleHandle, ref ConsoleInfo);
    OriginalColors = ConsoleInfo.wAttributes;
    }

    public void TextColor(int color)
    {
    SetConsoleTextAttribute(hConsoleHandle, color);
    }

    public void ResetColor()
    {
    SetConsoleTextAttribute(hConsoleHandle, OriginalColors);
    }
    }
    }
  4. 粘贴下面的代码示例包含 Main 函数的类文件中。验证的代码示例将替换现有代码文件中的所有
       using System;

    namespace ConsoleColor
    {
    class Class1
    {
    [STAThread]
    static void Main(string[] args)
    {
    Class2 TextChange = new Class2();
    Console.WriteLine("Original Colors");
    Console.WriteLine("Press Enter to Begin");
    Console.ReadLine();
    TextChange.TextColor((int)Class2.Foreground.Green +
    (int)Class2.Foreground.Intensity);
    Console.WriteLine("THIS TEXT IS GREEN");
    Console.WriteLine("Press Enter to change colors again");
    Console.ReadLine();
    TextChange.TextColor((int)Class2.Foreground.Red +
    (int)Class2.Foreground.Blue +
    (int)Class2.Foreground.Intensity);
    Console.WriteLine("NOW THE TEXT IS PURPLE");
    Console.WriteLine("Press Enter to change colors again");
    Console.ReadLine();
    TextChange.TextColor((int)Class2.Foreground.Blue +
    (int)Class2.Foreground.Intensity +
    (int)Class2.Background.Green +
    (int)Class2.Background.Intensity);
    Console.WriteLine("NOW THE TEXT IS BLUE AND BACKGROUND OF IT IS GREEN");
    Console.WriteLine("Press Enter change everything back to normal");
    Console.ReadLine();
    TextChange.ResetColor();
    Console.WriteLine("Back to Original Colors");
    Console.WriteLine("Press Enter to Terminate");
    Console.ReadLine();
    }
    }
    }
  5. 按 f5 键编译并运行该程序。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值