c# - 如何将我的标题基于窗口的宽度 W/O 不必重复编写相同的代码

标签 c# user-interface position cursor

我要拔头发了。我一直在努力让它工作,但我能想出的只是一个顶部看起来像这样的控制台屏幕:

+-------------------------------------------------------------------------------------------+
                                                                                            +
                                                                                            +
                                                                                            +
                                                                                            +
                                                                                            +
Are you ready to play Hangman? yes/no:

我想做的是在顶部矩形的短边(长 6 行)的四个角上分别有“+”和“----”底部(就像它目前在顶部一样)。我已经用 40 种不同的方式写了这个东西,但我似乎总是得到相同的结果。而且看起来它应该比写下每一行臭线更容易。

这是我的代码(我隐藏了代码中不需要的部分):

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hangman
{
class Program
{

    protected static int firstColumn;
    protected static int firstRow;


     protected static void headerWindow( string border, int posX, int posY)
    {
        try
        {
            Console.SetCursorPosition(firstColumn + posX, firstRow + posY);
            Console.Write(border);

        }
         catch(ArgumentOutOfRangeException error)
        {
            Console.Clear();
            Console.Write(error.Message);

        }

    }

    //I created a method to perform this, so that I can have the program stay open for either 1) a new game or 2) just to see if I could do it. It works!
    private static void printWord()
    {
        String[] myWordArrays = File.ReadAllLines("WordList.txt");
        Random randomWord = new Random();
        //int lineCount = File.ReadLines("WordList.txt").Count();            
        int activeWord = randomWord.Next(0, myWordArrays.Length);
        string userSelection = "";

        Console.WriteLine("Are you Ready to play Hangman? yes/no: ");

        userSelection = Console.ReadLine();
            if(userSelection == "yes")
            {
                //This runs through the randomly chosen word and prints an underscore in place of each letter - it does work
                foreach(char letter in myWordArrays[activeWord])
                {
                    Console.Write("_ ");

                }

                //This prints to the console "Can you guess what this 'varyingLength' letter word is?" - it does work.
                Console.WriteLine("\n \nCan you guess what this "+ myWordArrays[activeWord].Length +" letter word is?");
                Console.ReadLine();
            } 
            else if(userSelection=="no")
            {
                Console.WriteLine("I'm sorry you feel that way. Press Enter to Exit the program!");
                Console.ReadLine();
            }

    }

    private static void headerFile()
    {
        Console.Clear();
        firstColumn = Console.CursorLeft;
        firstRow = Console.CursorTop;

        int columnNumber = Console.WindowWidth - 1;
        var xcoord = 0;
        var ycoord = 0;

        if(xcoord==0 && ycoord==0)
        {
            headerWindow("+",xcoord,ycoord);
            Console.Write("");
            xcoord++;
        }
     }



    static void Main(string[] args)
    {
        headerFile();
        printWord();
    }
}

}

我需要指出,如果您看到多个相同的东西,那是因为我已经注释并隐藏了长行代码(以防我以后想使用它)。它应该保持隐藏状态,但显然 Ctrl+A,C 甚至复制了 IDE 中折叠的文本。

最佳答案

试试这段代码,我认为它可以满足您的需要:

        for(int i = 0; i < columnNumber; i++) // Loop across long length
        {
            headerWindow("-", i, 0); // Top line
            headerWindow("-", i, 6); // Bottom line
        }
        for(int i = 0; i < 6; i++) // Loop across short length
        {
            headerWindow("|", 0, i); // Left side
            headerWindow("|", columnNumber, i); // Right side
        }
        // Draw over corners with "+"
        headerWindow("+", 0, 0);
        headerWindow("+", 0, 6);
        headerWindow("+", columnNumber, 0);
        headerWindow("+", columnNumber, 6);

输出:

+------------------------------------------------------------------------------+
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
+------------------------------------------------------------------------------+

它打算进入 headerFile()。此外,您应该将 6 替换为名为 HEADER_HEIGHT 或类似内容的常量,以便于修改和提高可读性。

关于c# - 如何将我的标题基于窗口的宽度 W/O 不必重复编写相同的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31576351/

相关文章:

c# - 使用 yield 时在 try/catch 中包装对迭代器的调用

c# - Observable.FromAsync+Repeat+TakeWhile 的组合创建无限循环

WPF UI 建议?

c++ - 移动 NamedWindow 时 OpenCV 崩溃

iPhone UI 库?

c# - MVC EF 不断寻找 userId,这很昂贵

c# - PyEphem(日出/日落时间计算)等价于 C#

r - GG图 : Two stacked bar plots side by side (not facets)

css - 带有图像的多个 div 的绝对定位的 Chrome/IE 问题

javascript - 在 div HTML 中定位多个 SVG 元素