use*_*923 5 .net c# console console-application extended-ascii
我需要在C#控制台应用程序中使用扩展的ASCII绘制一个内部带有数字的矩形.我该怎么办呢?
这是一个演示.
小智 11
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();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
这是 String 的扩展方法,它将在给定字符串周围绘制一个控制台框。包括多行支持。
\n即\nstring tmp = "some value"; Console.Write(tmp.DrawInConsoleBox());
public static string DrawInConsoleBox(this string s)\n {\n string ulCorner = "\xe2\x95\x94";\n string llCorner = "\xe2\x95\x9a";\n string urCorner = "\xe2\x95\x97";\n string lrCorner = "\xe2\x95\x9d";\n string vertical = "\xe2\x95\x91";\n string horizontal = "\xe2\x95\x90";\n\n string[] lines = s.Split(new char[] { \'\\r\', \'\\n\' }, StringSplitOptions.RemoveEmptyEntries);\n \n\n int longest = 0;\n foreach(string line in lines)\n {\n if (line.Length > longest)\n longest = line.Length;\n }\n int width = longest + 2; // 1 space on each side\n\n \n string h = string.Empty;\n for (int i = 0; i < width; i++)\n h += horizontal;\n\n // box top\n StringBuilder sb = new StringBuilder();\n sb.AppendLine(ulCorner + h + urCorner);\n\n // box contents\n foreach (string line in lines)\n {\n double dblSpaces = (((double)width - (double)line.Length) / (double)2);\n int iSpaces = Convert.ToInt32(dblSpaces);\n\n if (dblSpaces > iSpaces) // not an even amount of chars\n {\n iSpaces += 1; // round up to next whole number\n }\n\n string beginSpacing = "";\n string endSpacing = "";\n for (int i = 0; i < iSpaces; i++)\n {\n beginSpacing += " ";\n\n if (! (iSpaces > dblSpaces && i == iSpaces - 1)) // if there is an extra space somewhere, it should be in the beginning\n {\n endSpacing += " ";\n }\n }\n // add the text line to the box\n sb.AppendLine(vertical + beginSpacing + line + endSpacing + vertical);\n }\n\n // box bottom\n sb.AppendLine(llCorner + h + lrCorner);\n\n // the finished box\n return sb.ToString();\n }\nRun Code Online (Sandbox Code Playgroud)\n\n
| 归档时间: |
|
| 查看次数: |
32008 次 |
| 最近记录: |