如何在C#控制台应用程序中绘制框,矩形 [英] How to Draw Box,Rectangle in a C# Console application

查看:221
本文介绍了如何在C#控制台应用程序中绘制框,矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要求2个相关问题。

1 - 我们如何将输出(如结果和消息)放在c#控制台应用程序的一个框内。

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

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

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

推荐答案

假设你只是一个字符框,这将做到这一点。

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.

这篇关于如何在C#控制台应用程序中绘制框,矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆