7

C#.NET4コンソールアプリでのテキストの中央揃えに問題があります。

これは、テキストを中央に配置するための私の方法です。

private static void centerText(String text)
{
    int winWidth = (Console.WindowWidth / 2);
    Console.WriteLine(String.Format("{0,"+winWidth+"}", text));
}

ただし、通常どおりに出力されるので、出力を取得するだけです。ただし、この行を使用する場合:

Console.WriteLine(String.Format("{0,"+winWidth+"}", "text"));

「テキスト」は必要に応じて中央に配置されます。

私はcenterTextこれらの2つのメソッドで呼び出しています:

private static void drawStars()
{
    centerText("*********************************************");
}
private static void title(string location)
{
    drawStars();
    centerText("+++ Du er nu her: " + location + "! +++");
    drawStars();
}
4

3 に答える 3

17

代わりにこれを試してください:

private static void centerText(String text)
{
    Console.Write(new string(' ', (Console.WindowWidth - text.Length) / 2));
    Console.WriteLine(text);
}

最初のコードの問題は、テキストが画面の中央から始まることでした。テキストの中心をそこに配置する必要があります。

このように段落全体を中央揃えで印刷する場合は、もう少し作業を行う必要があります。

于 2012-10-11T20:29:21.677 に答える
3

コンソールヘッダーを呼び出すための独自のメソッドがあります。

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                                       ║
╚════════════════════════════════════════════════════════════════════════════════════════╝

90で置き換えることができwindowsWidthますConsole.WindowWidth

更新-2019年2月-コードがクリーンアップされ、動的サイズになりました

/// <summary>
/// Application header, also sets the console title
/// </summary>
/// <param name="title">Title of application</param>
/// <param name="subtitle">Subtitle of application</param>
/// <param name="foreGroundColor">Foreground color</param>
public static void Header(string title, string subtitle = "", ConsoleColor foreGroundColor = ConsoleColor.White, int windowWidthSize = 90)
{
    Console.Title = title + (subtitle != "" ? " - " + subtitle : "");
    string titleContent = CenterText(title, "║");
    string subtitleContent = CenterText(subtitle, "║");
    string borderLine = new String('═', windowWidthSize - 2);

    Console.ForegroundColor = foreGroundColor;
    Console.WriteLine($"╔{borderLine}╗");
    Console.WriteLine(titleContent);
    if (!string.IsNullOrEmpty(subtitle))
    {
        Console.WriteLine(subtitleContent);
    }
    Console.WriteLine($"╚{borderLine}╝");
    Console.ResetColor();
}

/// <summary>
/// Align content to center for console. Can be used with decoration if used inside menu or header
/// </summary>
/// <param name="content">Content to center</param>
/// <param name="decorationString">Left and right decoration, default is empty/none</param>
/// <returns>Center aligned text</returns>
public static string CenterText(string content, string decorationString = "", int windowWidthSize = 90)
{
    int windowWidth = windowWidthSize - (2 * decorationString.Length);
    return String.Format(decorationString + "{0," + ((windowWidth / 2) + (content.Length / 2)) + "}{1," + (windowWidth - (windowWidth / 2) - (content.Length / 2) + decorationString.Length) + "}", content, decorationString);
}
于 2018-02-12T20:55:41.263 に答える
2

渡されるテキストには、などの空白が含まれている可能性があります\r\n。その後、次のような書き込みを呼び出す前に空白を削除してください。

string textClean = Regex.Replace(text, @"([\r\n])", string.Empty);

// Then center on text clean 
于 2012-10-11T20:32:25.283 に答える