向控制台输出添加文本修饰
我有 ac# .net 3.5 应用程序,它使用 StreamWriter 将文本写入控制台。有没有办法向打印到控制台的文本添加下划线和删除线等文本装饰?可能使用 ANSI 转义序列?
TextWriter writer = new StreamWriter(Console.OpenStandardOutput());
writer.WriteLine("some underlined text");
谢谢, 保罗·H
I have a c# .net 3.5 application that writes text to the console using a StreamWriter. Is there a way I can add text decorations like underline and strikethrough to the text that is printed to the console? Possibly using ANSI escape sequences?
TextWriter writer = new StreamWriter(Console.OpenStandardOutput());
writer.WriteLine("some underlined text");
Thanks,
PaulH
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在 Windows 10 内部版本 16257 及更高版本中:
In Windows 10 build 16257 and later:
我用这个代码。这是 Vladimir Reshetnikov 答案的固定版本,使用正确的转义码进行重置。
这将执行带下划线的文本,并且具有不重置您设置的任何颜色的优点。
I use this code. It's a fixed version of Vladimir Reshetnikov's answer, using the correct escape code for the reset.
This will do underlined text, and has the benefit of not resetting any colors you have set.
Windows 控制台不支持 ANSI 转义序列。据我所知,更改输出字符属性的唯一方法是在写入字符之前调用
SetConsoleTextAttribute
。或者,在 .NET 中,修改Console.ForegroundColor
或Console.BackgroundColor
属性。可以通过类型转换将这些属性设置为自定义值(即不是由
ConsoleColor
定义的值)。但我不知道这对你有什么好处。我不知道我在 Windows 控制台上见过带删除线的文本,而且我已经很多年没有看到下划线了。我想这是可能的,但我不知道它是如何做到的。
The Windows console does not support ANSI escape sequences. To my knowledge, the only way to change the attributes of an output character is to call
SetConsoleTextAttribute
before writing the character. Or, in .NET, modify theConsole.ForegroundColor
orConsole.BackgroundColor
attributes.It might be possible to set those properties to custom values (i.e. values not defined by
ConsoleColor
) with a type cast. But I don't know what good that would do you.I don't know that I've ever seen strikethrough text on a Windows console, and it's been years since I saw underline. I suppose it's possible, but I don't know how it's done.
简短的回答,不;控制台不允许在输出中使用下划线字符。
更长的答案:控制台使用的屏幕缓冲区只不过是一个字节数组。每个光标位置都是一个字节或一个字符。要创建下划线,您需要两个字符重叠(这在控制台中是不可能的),或者您需要访问使用上部 128 个字符值作为下部 128 个字符值的下划线或删除线版本的代码页(我不知道一个)。
如果您愿意对带有下划线的行使用“双倍行距”,则可以解决此问题。字符代码 0x00AF(十进制 175)是一个“文本艺术”字符,表示字符空间顶部的边框。如果您在文本下方的行中使用这些内容,则会出现下划线。
Short answer, no; the console doesn't allow the use of underlined characters in output.
Longer answer: The screen buffer used by the console is little more than a byte array. Each cursor position is one byte or one character. To create an underline, you either need two characters overlapping (which isn't possible in the console), or you need access to a codepage that uses the upper 128 character values as underlined or strikethrough versions of the lower 128 (I don't know of one).
You can work around this if you are willing to go "double-spaced" for lines that have underlines. Character code 0x00AF (decimal 175) is a "text art" character representing a border across the top of the character space. If you use those in the line underneath your text, presto, underlines.
我发现了这个问题,并想我应该使用 kernel32 函数添加到 Windows 10 之前的终端的答案中,
在我的 PC 上输出
I found this question and thought I would add to the answers for pre-windows 10 terminal using kernel32 functions,
Output on my PC
更改控制台的前景色/背景色非常容易: http://www.dotnetperls.com/console- color 但据我所知,例如,无法放置一些粗体文本。但我并没有真正尝试实现这一目标,所以我不确定。
It's pretty easy to change the foreground/background color of console : http://www.dotnetperls.com/console-color but AFAIK there is no way to put some bold text, for example. But I didn't really tried to achieve that so i'm not sure.
对于未来的查看者来说,在更高的 .NET 版本中不再需要 API 调用。
.NET 4.8 中的以下内容按原样完美运行,无需 extern。
Just for future viewers, the API calls are not needed anymore under later .NET versions.
The following in .NET 4.8 works perfectly as is, no extern required.