转:c#:更改控制台文字输出颜色

链接:http://support.microsoft.com/kb/319883/zh-cn

简介

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

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

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

分步示例

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

 

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

 

有关详细的信息,请访问下面的 Microsoft 网站:

控制台功能
http://msdn2.microsoft.com/en-us/library/ms682073.aspx (http://msdn2.microsoft.com/en-us/library/ms682073.aspx)  

有关更多的信息请单击下面文章编号,以查看 Microsoft 知识库中相应的文章:

319257  (http://support.microsoft.com/kb/319257/EN-US/ ) 如何: 清除与 Visual C#.net 控制台窗口
点击这里察看该文章的英文版: 319883 

posted on 2010-03-19 10:05  I过T  阅读(5941)  评论(1编辑  收藏  举报

导航