C#下的控制台程序,通常是黑色和白色的屏幕,是否可以改变它们呢?答案是肯定的,可以通过Win32下的API函数
SetConsoleTextAttribute()去改变控制台程序的前景色和背景色。
SetConsoleTextAttribute函数需要两个参数:
1.
控制台屏幕缓冲的句柄;
2.
字符属性。
BOOL SetConsoleTextAttribute(
HANDLE hConsoleOutput,
WORD wAttributes
);
HANDLE hConsoleOutput,
WORD wAttributes
);
通过Win32下的另外一个API函数
GetStdHandle()可以获得控制台屏幕缓冲的句柄,此函数需要一个参数同时返回值为一个输入、输出或者错误设备的句柄。GetStdHandle函数的参数赋值分别是:输入设备为-10,输出设备为-11,错误设备为-12。还有针对前景色和背景色的特性,如0x0001对应前景色为蓝色,0x0010对应背景色为蓝色。
C#下如何使用Win32的API函数
首先,函数的所有声明都应该使用DllImport特性。API函数一定要声明为static extern(静态、外部实现)。我们看下面这个例子。
using
System;
using System.Runtime.InteropServices; // for DllImport attribute
namespace color_console
... {
class Class1
...{
static void Main(string[] args)
...{
Class1 c =new Class1();
c.change();
}
[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);
/**//*declaring the getstdhandle funtion to get thehandle that would be used in setConsoletextattribute function */
void change()
...{
IntPtr hOut; /**//* declaring varianle to get handle*/
hOut= GetStdHandle(-11);/**//* -11 is sent for output device*/
/**//*Displaying text in different colors and background colors*/
SetConsoleTextAttribute(hOut, CharacterAttributes.FOREGROUND_BLUE );
Console.WriteLine(" Subhan ALLAH ");
SetConsoleTextAttribute(hOut, CharacterAttributes.BACKGROUND_RED);
Console.WriteLine(" Alkhamdolillah ");
SetConsoleTextAttribute(hOut, CharacterAttributes.BACKGROUND_GREEN );
Console.WriteLine(" Allah O Akbar ");
SetConsoleTextAttribute(hOut, CharacterAttributes.FOREGROUND_RED );
Console.WriteLine(" Pakistan ");
}
/**//* This enumeration lists all of the character attributes. You can combine attributes to achieve specific effects.*/
public enum CharacterAttributes
...{
FOREGROUND_BLUE = 0x0001,
FOREGROUND_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
}
}
using System.Runtime.InteropServices; // for DllImport attribute
namespace color_console
... {
class Class1
...{
static void Main(string[] args)
...{
Class1 c =new Class1();
c.change();
}
[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);
/**//*declaring the getstdhandle funtion to get thehandle that would be used in setConsoletextattribute function */
void change()
...{
IntPtr hOut; /**//* declaring varianle to get handle*/
hOut= GetStdHandle(-11);/**//* -11 is sent for output device*/
/**//*Displaying text in different colors and background colors*/
SetConsoleTextAttribute(hOut, CharacterAttributes.FOREGROUND_BLUE );
Console.WriteLine(" Subhan ALLAH ");
SetConsoleTextAttribute(hOut, CharacterAttributes.BACKGROUND_RED);
Console.WriteLine(" Alkhamdolillah ");
SetConsoleTextAttribute(hOut, CharacterAttributes.BACKGROUND_GREEN );
Console.WriteLine(" Allah O Akbar ");
SetConsoleTextAttribute(hOut, CharacterAttributes.FOREGROUND_RED );
Console.WriteLine(" Pakistan ");
}
/**//* This enumeration lists all of the character attributes. You can combine attributes to achieve specific effects.*/
public enum CharacterAttributes
...{
FOREGROUND_BLUE = 0x0001,
FOREGROUND_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
}
}
改变控制台程序的标题
改变控制台的标题也非常容易,只要通过SetConsoleTitle()函数,然后提供一个字符串作为参数给它,此字符串就成为控制台新的标题,是不是很容易?
[DllImport(
"
kernel32.dll
"
)
public static extern bool SetConsoleTitle(String lpConsoleTitle);
public static extern bool SetConsoleTitle(String lpConsoleTitle);
所以大家已经看到在C#下通过win32的API可以做很多
.NET Framework做不到的事情。当然也可以通过win32的API改变控制台应用程序的字体和鼠标。
本人第一次翻译关于C#的文章,不足之处各位见谅!!!