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

发布于 2024-09-26 05:38:31 字数 111 浏览 1 评论 0原文

我问2个相关问题。

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

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

I ask for 2 related questions.

1-How we can Put outputs(such as Results and Messages) inside a box in a c# console application.

2-How we can draw rectangle in a c# console application.thank u for any sample tutorial or advice

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆萌少年 2024-10-03 05:38:31

假设你只是指一个字符框,这就可以了。

 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);
                }


            }

        }

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

Assuming you just meant a character box this will do it.

 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);
                }


            }

        }

I edited the code to put a message in their. You will need to do more work on the boundary conditions. Ex no space in the message a word that is longer then the box but this should be enough to get you started.

花海 2024-10-03 05:38:31

C# 有 curses 绑定(这可能是一个好的开始):http://curses-sharp.sourceforge.net/

There are curses bindings for C# (that might be a good start): http://curses-sharp.sourceforge.net/

嘿哥们儿 2024-10-03 05:38:31

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

If you want to write it by yourself, You can use extended ascii code to draw simple shapes in a console. Extended AScii Table

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文