如何在控制台应用程序中绘制一个矩形?
Posted
技术标签:
【中文标题】如何在控制台应用程序中绘制一个矩形?【英文标题】:How to draw a rectangle in console application? 【发布时间】:2011-08-25 18:49:20 【问题描述】:我需要在 C# 控制台应用程序中使用扩展的 ASCII 绘制一个矩形,里面有一个数字。我该怎么办?
这是一个演示。
【问题讨论】:
Console.Write() - display extended ascii chars? 的可能重复项 C# 不使用 ASCII、扩展或其他方式。 【参考方案1】:public class ConsoleRectangle
private int hWidth;
private int hHeight;
private Point hLocation;
private ConsoleColor hBorderColor;
public ConsoleRectangle(int width, int height, Point location, ConsoleColor borderColor)
hWidth = width;
hHeight = height;
hLocation = location;
hBorderColor = borderColor;
public Point Location
get return hLocation;
set hLocation = value;
public int Width
get return hWidth;
set hWidth = value;
public int Height
get return hHeight;
set hHeight = value;
public ConsoleColor BorderColor
get return hBorderColor;
set hBorderColor = value;
public void Draw()
string s = "╔";
string space = "";
string temp = "";
for (int i = 0; i < Width; i++)
space += " ";
s += "═";
for (int j = 0; j < Location.X ; j++)
temp += " ";
s += "╗" + "\n";
for (int i = 0; i < Height; i++)
s += temp + "║" + space + "║" + "\n";
s += temp + "╚";
for (int i = 0; i < Width; i++)
s += "═";
s += "╝" + "\n";
Console.ForegroundColor = BorderColor;
Console.CursorTop = hLocation.Y;
Console.CursorLeft = hLocation.X;
Console.Write(s);
Console.ResetColor();
【讨论】:
如何使用它,如果我需要在矩形内绘制数字 如果您在整个控制台区域周围绘制一个框,请将您的宽度和高度减少两倍(因为每个维度中的第一个和最后一个字符不可用/不可用,被角落占据个字符)。 有什么办法可以修改这个,这样你就可以画两个矩形(一个在另一个里面)?由于间隔第二个矩形覆盖第一个矩形左水平线 如果要复制/粘贴:╔ ═ ╗ ╚ ╝
@Alex,你忘了║
【参考方案2】:
喜欢this?
这对我有用:
Console.OutputEncoding = Encoding.GetEncoding(866);
Console.WriteLine("┌─┐");
Console.WriteLine("│1│");
Console.WriteLine("└─┘");
[编辑]
回答评论中的子问题:
Console.OutputEncoding = Encoding.GetEncoding(866);
Console.WriteLine(" ┌─┐");
Console.WriteLine(" │1│");
Console.WriteLine("┌─┼─┘");
Console.WriteLine("│1│");
Console.WriteLine("└─┘");
【讨论】:
@Alex 感谢您的示例。这绝对可以解决问题。如果我错了,请更正,但您所做的不是使用 ascii 吗?“Encoding.GetEncoding(866) 有什么作用? @Alex 你是怎么画出矩形的顶部的? @user712923 - 按住 Alt 键并在数字键盘上键入 218。这将显示┌。比你对 196 和 191 做同样的事情。重要的是 - 你需要使用数字键盘(NumLock 开启)。 @Alex 你非常有帮助。我能问你最后一件事吗。对不起。我已经完成了矩形,我需要在右上角绘制另一个矩形,里面有一个数字。任何帮助的机会?谢谢【参考方案3】:这是对 String 的扩展方法,它将围绕给定的字符串绘制一个控制台框。包括多行支持。
即
string tmp = "some value"; Console.Write(tmp.DrawInConsoleBox());
public static string DrawInConsoleBox(this string s)
string ulCorner = "╔";
string llCorner = "╚";
string urCorner = "╗";
string lrCorner = "╝";
string vertical = "║";
string horizontal = "═";
string[] lines = s.Split(new char[] '\r', '\n' , StringSplitOptions.RemoveEmptyEntries);
int longest = 0;
foreach(string line in lines)
if (line.Length > longest)
longest = line.Length;
int width = longest + 2; // 1 space on each side
string h = string.Empty;
for (int i = 0; i < width; i++)
h += horizontal;
// box top
StringBuilder sb = new StringBuilder();
sb.AppendLine(ulCorner + h + urCorner);
// box contents
foreach (string line in lines)
double dblSpaces = (((double)width - (double)line.Length) / (double)2);
int iSpaces = Convert.ToInt32(dblSpaces);
if (dblSpaces > iSpaces) // not an even amount of chars
iSpaces += 1; // round up to next whole number
string beginSpacing = "";
string endSpacing = "";
for (int i = 0; i < iSpaces; i++)
beginSpacing += " ";
if (! (iSpaces > dblSpaces && i == iSpaces - 1)) // if there is an extra space somewhere, it should be in the beginning
endSpacing += " ";
// add the text line to the box
sb.AppendLine(vertical + beginSpacing + line + endSpacing + vertical);
// box bottom
sb.AppendLine(llCorner + h + lrCorner);
// the finished box
return sb.ToString();
这样的输出
【讨论】:
【参考方案4】:您可以使用CsConsoleFormat† 在控制台中使用 ASCII 边框符号进行绘制。
在带有“双”线的矩形内绘制一个数字:
ConsoleRenderer.RenderDocument(
new Document()
.AddChildren(
new Border
Stroke = LineThickness.Wide,
Align = HorizontalAlignment.Left
.AddChildren(1337)
)
);
您可以更改Stroke = LineThickness.Wide
线以更改线的样式。 LineThickness.Single
会产生细单线,new LineThickness(LineWidth.Single, LineWidth.Wide)
会产生单垂直和双水平线。
它是这样的:
您还可以使用ConsoleBuffer
类显式绘制线条(为清楚起见添加了参数名称):
using static System.ConsoleColor;
var buffer = new ConsoleBuffer(width: 6);
buffer.DrawHorizontalLine(x: 0, y: 0, width: 6, color: White);
buffer.DrawHorizontalLine(x: 0, y: 2, width: 6, color: White);
buffer.DrawVerticalLine(x: 0, y: 0, height: 3, color: White);
buffer.DrawVerticalLine(x: 5, y: 0, height: 3, color: White);
buffer.DrawString(x: 1, y: 1, color: White, text: "1337");
new ConsoleRenderTarget().Render(buffer);
† CsConsoleFormat 是我开发的。
【讨论】:
以上是关于如何在控制台应用程序中绘制一个矩形?的主要内容,如果未能解决你的问题,请参考以下文章
当我绘制另一个矩形时,如何保留我在 Jpanel 上绘制的矩形?
如何在 Android Canvas 中绘制带有文本的矩形按钮?