我有我自己的方法調用控制檯頭前:
public static void Header(string title, string subtitle = "", ConsoleColor color = ConsoleColor.White)
{
int windowWidth = 90 - 2;
string titleContent = String.Format("║{0," + ((windowWidth/2) + (title.Length/2)) + "}{1," + (windowWidth - (windowWidth/2) - (title.Length/2) + 1) + "}", title, "║");
string subtitleContent = String.Format("║{0," + ((windowWidth/2) + (subtitle.Length/2)) + "}{1," + (windowWidth - (windowWidth/2) - (subtitle.Length/2) + 1) + "}", subtitle, "║");
Console.WriteLine("╔════════════════════════════════════════════════════════════════════════════════════════╗");
Console.WriteLine(titleContent);
if (!string.IsNullOrEmpty(subtitle))
{
Console.WriteLine(subtitleContent);
}
Console.WriteLine("╚════════════════════════════════════════════════════════════════════════════════════════╝");
}
然後你這樣稱呼它YourStaticClass.Header("Test", "Version 1.0");
它應該看起來像這樣:
╔════════════════════════════════════════════════════════════════════════════════════════╗
║ Test ║
║ Version 1.0 ║
╚════════════════════════════════════════════════════════════════════════════════════════╝
您可以Console.WindowWidth
當/因爲你的文字較長屏幕的寬度的一半將出現問題更換
windowsWidth
的90
。 – DaveShaw@DaveShaw還有一個比這個更大的問題:文本寬度甚至從未成爲居中的一部分。 –