如何在 C# 控制台应用程序中绘制盒子、矩形

3

我有两个相关问题。

1-在C#控制台应用程序中,我们如何将输出(例如结果和消息)放入框中。

2-在C#控制台应用程序中,我们如何绘制矩形。感谢任何示例教程或建议。

3个回答

4
假设你只是指一个字符框,那么下面的代码就可以实现它。
 private static void DrawABox( int x, int y, int width, int height,char Edge,string Message )
    {
        int LastIndex =0 ;
        Console.SetCursorPosition(x, y);
        for ( int h_i = 0; h_i <= height ; h_i++ )
        {
            if ( LastIndex != -1 )
            {
                int seaindex = (LastIndex + ( width - 1) );
                if(seaindex >= Message.Length -1 )
                    seaindex = Message.Length - 1;
                int newIndex = Message.LastIndexOf(' ',seaindex);
                if(newIndex == -1 )
                    newIndex = Message.Length - 1;
                string substr = Message.Substring(LastIndex, newIndex - LastIndex);
                LastIndex = newIndex;
                Console.SetCursorPosition(x + 1, y + h_i);
                Console.Write(substr);
            }
            for ( int w_i = 0; w_i <= width; w_i++ )
            {

                if ( h_i % height == 0 || w_i % width == 0 )
                {
                    Console.SetCursorPosition(x + w_i, y + h_i);
                    Console.Write(Edge);
                }


            }

        }

我编辑了代码并添加了一条消息。你需要在边界条件上做更多的工作,例如:消息中没有空格、单词比框还长等。但这应该足以让你开始了。


好的观点,乍一看,我觉得我懂了,谢谢。但我需要更多地研究这个问题,以得到一个 solid 的解决方案,然后再发布它。 - siamak

3

0

如果你想自己编写,可以使用扩展ASCII码在控制台中绘制简单的形状。扩展ASCII表


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接