C# 如何在控制台应用程序中绘制矩形?
我需要在C#console应用程序中,使用扩展ASCII绘制一个内部带有数字的矩形。我该怎么做呢 这是演示用的。喜欢吗 这对我很有用:C# 如何在控制台应用程序中绘制矩形?,c#,.net,console,console-application,extended-ascii,C#,.net,Console,Console Application,Extended Ascii,我需要在C#console应用程序中,使用扩展ASCII绘制一个内部带有数字的矩形。我该怎么做呢 这是演示用的。喜欢吗 这对我很有用: Console.OutputEncoding = Encoding.GetEncoding(866); Console.WriteLine("┌─┐"); Console.WriteLine("│1│"); Console.WriteLine("└─┘"); [编辑] 对评论中的子问题的答复: Console.OutputEncoding = Encoding
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("└─┘");
公共类控制台矩形
{
私人国际机场;
私人国际机场;
专用点定位;
专用控制台颜色hBorderColor;
公共控制台矩形(整数宽度、整数高度、点位置、控制台颜色边框颜色)
{
hWidth=宽度;
hhheight=高度;
位置=位置;
hBorderColor=边框颜色;
}
公共点位置
{
获取{return hLocation;}
设置{hLocation=value;}
}
公共整数宽度
{
获取{return hWidth;}
设置{hWidth=value;}
}
公共内部高度
{
获取{return hhheight;}
设置{hHeight=value;}
}
公共控制台颜色边框颜色
{
获取{return hBorderColor;}
设置{hBorderColor=value;}
}
公众抽签()
{
字符串s=”╔";
字符串空格=”;
字符串temp=“”;
对于(int i=0;i
这是字符串的扩展方法,它将在给定字符串周围绘制一个控制台框。包括多行支持
i、 e。
string tmp=“some value”;
Console.Write(tmp.drawincolebox());
公共静态字符串DrawInColeBox(此字符串为s)
{
字符串ulCorner=”╔";
字符串llCorner=”╚";
字符串urCorner=”╗";
字符串lrCorner=”╝";
字符串垂直=”║";
字符串水平=”═";
string[]line=s.Split(新字符[]{'\r','\n'},StringSplitOptions.RemoveEmptyEntries);
int=0;
foreach(行中的字符串行)
{
如果(line.Length>最长)
最长=直线长度;
}
int width=longest+2;//每边有1个空格
字符串h=string.Empty;
对于(int i=0;iiSpaces)//不是偶数个字符
{
ispace+=1;//向上取整到下一个整数
}
字符串beginspace=“”;
字符串结束间距=”;
对于(int i=0;idblSpaces&&i==iSpaces-1))//如果某个地方有额外的空间,它应该在开头
{
端间距+=“”;
}
}
//将文本行添加到框中
sb.追加行(垂直+起始间距+行+结束间距+垂直);
}
//箱底
sb.附加线(左上角+h+左下角);
//成品盒
使某人返回字符串();
}
您可以使用†在控制台中使用ASCII边框符号绘制
用“双线”在矩形内绘制数字:
您可以更改Stroke=LineThickness.Wide
line以更改线条样式。LineThickness.Single
将生成单条细线,newlinethickness(LineWidth.Single,LineWidth.Wide)
将生成单条垂直线和双水平线
下面是它的样子:
您还可以使用ConsoleBuffer
类显式地绘制线条(为清晰起见添加了参数名称):
†CsConsoleFormat是我开发的。C的可能副本#不使用ASCII、扩展或其他形式。@Alex感谢您的示例。这肯定会起作用。如果我错了,请更正,但您没有使用ASCII是吗?还有什么是“Encoding.GetEncoding(866))是吗?@Alex您是如何绘制矩形的顶部的?@user712923-按住Alt键并在数字键盘上键入218。这将显示┌. 而不是196和191。重要提示-你需要使用数字键盘(NumLock已打开)。@Alex你一直非常乐于助人。我能问你最后一件事吗。对不起。我已经画好了矩形,我需要在右上角画另一个矩形,里面有一个数字。有什么帮助吗?谢谢你用它吗,如果我需要在矩形内绘制数字如果你在整个控制台区域周围绘制一个方框,请将宽度和高度减少两个(因为每个维度中的第一个和最后一个字符都不可用/被角字符占用)。是否有办法修改此项,以便你可以绘制两个矩形(一个在另一个内) ? 由于间隔较大,如果要复制/粘贴,第二个矩形将覆盖第一个矩形左水平线:
╔ ═ ╗ ╚ ╝
@Alex,你忘了║代码>
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();
}
}
ConsoleRenderer.RenderDocument(
new Document()
.AddChildren(
new Border {
Stroke = LineThickness.Wide,
Align = HorizontalAlignment.Left
}
.AddChildren(1337)
)
);
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);