如何在控制台应用程序中绘制矩形?
我需要在 C# 控制台应用程序中并使用扩展 ASCII 绘制一个矩形,内部有一个数字。我该怎么办?
这是一个演示。
I need to draw a rectangle, with a number inside, in a C# console app and using extended ASCII. How do I go about it?
This is for a demo.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是 String 的扩展方法,它将在给定字符串周围绘制一个控制台框。包括多行支持。
IE
string tmp = "某个值"; Console.Write(tmp.DrawInConsoleBox());
输出如下

This is an extension method to String, which will draw a console box around a given string. Multi-line support included.
i.e.
string tmp = "some value"; Console.Write(tmp.DrawInConsoleBox());
Output like this

喜欢这个?
这对我有用:
[编辑]
回答评论中的子问题:
Like this?
This worked for me:
[EDIT]
Answer to the sub-question in the comment:
您可以使用 CsConsoleFormat† 在控制台中使用 ASCII 边框符号进行绘制。
在具有“双”线的矩形内绘制数字:
您可以更改
Stroke = LineThickness.Wide
线来更改线的样式。LineThickness.Single
将生成细单线,new LineThickness(LineWidth.Single, LineWidth.Wide)
将生成单垂直线和双水平线。它看起来像这样:
您还可以使用
ConsoleBuffer
类显式绘制线条(参数名称为清楚起见添加):† CsConsoleFormat 是我开发的。
You can use CsConsoleFormat† to draw with ASCII border symbols in console.
Drawing a number within a rectangle with "double" lines:
You can change
Stroke = LineThickness.Wide
line to change the style of lines.LineThickness.Single
would produce thin single lines,new LineThickness(LineWidth.Single, LineWidth.Wide)
would produce single vertical and double horizontal lines.Here's what it looks like:
You can also use
ConsoleBuffer
class to draw lines explicitly (argument names added for clarity):† CsConsoleFormat was developed by me.
上面代码的问题是多余的空格,如果你绘制多个矩形,它会导致混乱。
这是一个递归绘制矩形而没有额外空格的代码。
Problem with above code is extra spaces, if you draw multiple rectangles, it causes mess.
here is a code which draw rectangles recursively without extra spaces.