How to Draw Box,Rectangle in a C# Console applicat

2019-04-10 18:16发布

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

3条回答
一纸荒年 Trace。
2楼-- · 2019-04-10 18:27

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

查看更多
冷血范
3楼-- · 2019-04-10 18:38

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

查看更多
男人必须洒脱
4楼-- · 2019-04-10 18:40

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.

查看更多
登录 后发表回答