简体   繁体   中英

Change Console text Color in C# after writing

我在C#控制台中打印了一些文本(很多),我想知道一次更改文本颜色而不清除控制台并以新颜色重新打印的最简单方法是什么,具有相同的效果作为C ++中的System(color ##)命令...预先感谢。

You can change the console output color using Console.BackgroundColor and Console.ForegroundColor properties. After you are done writing in the new color, use Console.ResetColor() to go back to defaults.

Changing the colors after the fact is a problem, because C# has no direct way to read text at a given position.You can rewrite it however, if you know what exactly is there, in a different color (first jumping to the location using Console.SetCursorPosition method and then writing over the original text).

If you want to be as efficient as possible, you will need a higher caliber in form of some P/Invoke wizadry. This is quite well described in the accepted answer to this similar question . The solution there takes advantage of writing the entire Console buffer at once, which is very fast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM