您的位置:首页 > 编程语言 > C#

如何使用 C# 来更改前景颜色和背景颜色,在控制台窗口中的文本的

2014-09-19 18:34 1076 查看
本分步指南介绍如何更改使用 Visual C# 写到控制台窗口的文本的前景色和背景色。

本文介绍如何保存控制台窗口的初始设置启动、 如何修改颜色设置,以及如何颜色还原为其原始值,为该程序则退出。


简介

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

在运行时更改这些属性时,所做的更改才有效,只要控制台窗口处于打开状态。如果您关闭并重新打开控制台窗口时,属性将重置为其默认值。如果您从命令行程序已经在运行的控制台窗口中运行此程序,,只要窗口处于打开状态,即使在您的程序退出之后,就对该控制台窗口有效的文本属性所做的更改。因此,程序应恢复原始的颜色属性之前,该程序则退出。

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


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


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


SetConsoleTextAttribute(hConsoleHandle, OriginalColors);



分步示例

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


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);
}
}
}


在类文件中包含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();
}
}
}


按 f5 键以编译并运行该程序。


故障排除

如果您遇到问题,请验证为TextColor方法中的参数传递一个有效的值。若要创建一个有效的值,组合定义的类中获得的前景色背景的枚举中的值。

转自:http://support.microsoft.com/kb/319883/zh-cn
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: