【发布时间】:2021-07-29 22:13:25
【问题描述】:
我正在尝试为控制台创建一个菜单。 使用 Visual Studio 2019、C#、控制台
目前运行良好,我为此使用了 Consolen.Helper。 我已经把自己导向这个 [Consolen.Helper 菜单](https://stackoverflow.com/questions/46908148/controlling-menu-with-the-arrow-keys-and-enter"First 最佳答案") 如果我使用纯文本,一切正常。 例子: 但我想多设计一点。
所以我可以通过 String @ "";以 Ascii 形式存入 但是当我并排显示选项时,它并没有真正起作用。 相互叠加效果很好。 例子:
为了让 ASCII 文本显示得很好,我已经把自己定位到了这个 [Ascii 新行](https://stackoverflow.com/questions/65076780/paste-ascii-art-in-center-of-console"Ascii 格式") 示例:
string start = @"
___ _ _
/ __| |_ __ _ _ _| |_
\__ \ _/ _` | '_| _|
|___/\__\__,_|_| \__|";
var linesstart = start.Split(new[] { Environment.NewLine },
StringSplitOptions.None);
var longestLengthstart = linesstart.Max(line => line.Length);
var leadingSpacesstart = new string(' ',longestLengthstart);
var startblock = string.Join(Environment.NewLine,
linesstart.Select(line => leadingSpacesstart + line));
但是当我并排显示选项时,它会覆盖所有内容,我不知道为什么。
我只是将其作为一种爱好,所以我不确切知道哪些信息很重要,以便有人可以帮助我 V_V。对不起
它应该看起来如何:
所以这里是所有的代码:
using System;
using System.Drawing;
using System.Linq;
using System.Text;
using ConsoleHelper;
namespace project
{
public class ConsoleHelper
{
public static void menu()
{
string start = @"
___ _ _
/ __| |_ __ _ _ _| |_
\__ \ _/ _` | '_| _|
|___/\__\__,_|_| \__|";
var linesstart = start.Split(new[] { Environment.NewLine },
StringSplitOptions.None);
var longestLengthstart = linesstart.Max(line => line.Length);
var leadingSpacesstart = new string(' ',longestLengthstart);
var startblock = string.Join(Environment.NewLine,
linesstart.Select(line => leadingSpacesstart + line));
string save = @"
___
/ __| __ ___ _____
\__ \/ _` \ V / -_)
|___/\__,_|\_/\___|";
var linessave = save.Split(new[] { Environment.NewLine },
StringSplitOptions.None);
var longestLengthsave = linessave.Max(line => line.Length);
var leadingSpacessave = new string(' ',longestLengthsave);
var saveblock = string.Join(Environment.NewLine,
linessave.Select(line => leadingSpacessave + line));
string load = @"
_ _
| | ___ __ _ __| |
| |__/ _ \/ _` / _` |
|____\___/\__,_\__,_|";
var linesload = load.Split(new[] { Environment.NewLine },
StringSplitOptions.None);
var longestLengthload = linesload.Max(line => line.Length);
var leadingSpacesload = new string(' ',longestLengthload);
var loadblock = string.Join(Environment.NewLine,
linesload.Select(line => leadingSpacesload + line));
string extra = @"
_
_____ _| |_ _ _ __ _
/ -_) \ / _| '_/ _` |
\___/_\_\\__|_| \__,_|";
var linesextra = extra.Split(new[] { Environment.NewLine },
StringSplitOptions.None);
var longestLengthextra = linesextra.Max(line => line.Length);
var leadingSpacesextra = new string(' ',longestLengthextra);
var extrablock = string.Join(Environment.NewLine,
linesextra.Select(line => leadingSpacesextra + line));
string credits = @"
_ _ _
__ _ _ ___ __| (_) |_ ___
/ _| '_/ -_) _` | | _(_-<
\__|_| \___\__,_|_|\__/__/";
var linescredits = credits.Split(new[] { Environment.NewLine },
StringSplitOptions.None);
var longestLengthcredits = linescredits.Max(line => line.Length);
var leadingSpacescredits = new string(' ',longestLengthcredits);
var creditsblock = string.Join(Environment.NewLine,
linescredits.Select(line => leadingSpacescredits + line));
int selectedClass = ConsoleHelper.MultipleChoice(true, startblock, saveblock, loadblock, extrablock, creditsblock);
switch (selectedClass)
{
case 0:
Console.Clear();
Console.WriteLine("0");
break;
case 1:
Console.Clear();
Console.WriteLine("1");
break;
case 2:
Console.Clear();
Console.WriteLine("2");
break;
case 3:
Console.Clear();
Console.WriteLine("3");
break;
case 4:
Console.Clear();
Console.WriteLine("4");
break;
}
}
public static int MultipleChoice (bool canCancel, params string[] options)
{
const int startX = 20;
const int startY = 0;
const int optionsPerLine = 2;
const int spacingPerLine = 5;
int currentSelection = 0;
ConsoleKey key;
Console.CursorVisible = false;
do
{
Console.Clear();
for (int i = 0; i < options.Length; i++)
{
Console.SetCursorPosition(startX + (i % optionsPerLine) * spacingPerLine,
startY + i / optionsPerLine);
if (i == currentSelection)
Console.ForegroundColor = Color.Red;
Console.Write(options[i]);
Console.ResetColor();
}
key = Console.ReadKey(true).Key;
switch (key)
{
case ConsoleKey.LeftArrow:
{
if (currentSelection % optionsPerLine > 0)
currentSelection--;
break;
}
case ConsoleKey.RightArrow:
{
if (currentSelection % optionsPerLine < optionsPerLine - 1)
currentSelection++;
break;
}
case ConsoleKey.UpArrow:
{
if (currentSelection >= optionsPerLine)
currentSelection -= optionsPerLine;
break;
}
case ConsoleKey.DownArrow:
{
if (currentSelection + optionsPerLine < options.Length)
currentSelection += optionsPerLine;
break;
}
case ConsoleKey.Escape:
{
if (canCancel)
return -1;
break;
}
}
} while (key != ConsoleKey.Enter);
Console.CursorVisible = true;
return currentSelection;
}
}
【问题讨论】:
-
那里有问题吗?
-
再次修改了帖子。但又来了。我无法让 Ascii 选项彼此相邻出现。然后,您总是会覆盖自己。所以重叠。
-
欢迎来到 SO。您能否将您的帖子 title 编辑为一个实际问题,而不是 一系列标签。谢谢。祝你好运!
-
如果您不能向我们展示实际代码,我们将无能为力。不过我会给你一个提示:考虑一下字符串中的所有空格,以及它们在控制台上的外观。