How can I write fast colored output to Console?

后端 未结 3 1499
猫巷女王i
猫巷女王i 2020-11-27 11:31

I want to learn if there is another (faster) way to output text to the console application window using C# .net than with the simple Write,

3条回答
  •  感情败类
    2020-11-27 12:02

    If you look at the implementation of Console's properties for altering console colours, they delegate to the SetConsoleTextAttribute method from kernel32.dll. This method takes character attributes as input to set both the foreground and background colours.

    From several MSDN doc pages, each screen buffer (of which a console has one) has a two-dimensional array of character info records, each represented by a CHAR_INFO. This is what determines the colour of each character. You can manipulate this using the SetConsoleTextAttribute method, but this is applied to any new text that is written to the console - you cannot manipulate existing text already on the console.

    Unless there is a lower-level hook into the console text colour properties (which doesn't look likely), I think you are stuck using these methods.


    One thing you could try is to create a new screen buffer, write to that, and then switch it to be the console's current buffer using SetConsoleActiveScreenBuffer. This may yield faster output as you will be writing all output to an inactive buffer.

提交回复
热议问题