在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
关键字:C# NET 控制台 前景色 背景色
This step-by-step article describes how to change the foreground and background colors of the text that is written to the Console window by using Visual C#. IntroductionTo change the foreground and background colors of text that the Console window displays, use the SetConsoleTextAttributeWin32 application programming interface (API) function. This function sets the attributes of the characters that are written to the screen buffer. OriginalColors = ConsoleInfo.wAttributes; SetConsoleTextAttribute(hConsoleHandle, color); You can use the ResetColor method to reset the output buffer attributes of the Console window to the original values that are captured when the program begins its execution. SetConsoleTextAttribute(hConsoleHandle, OriginalColors); Step-by-Step Example
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. Paste the following sample code in the class file that contains the Main function. Verify that the sample code replaces all of the existing code in the file. 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(); } } }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论