【问题标题】:How to Draw Box,Rectangle in a C# Console application如何在 C# 控制台应用程序中绘制方框、矩形
【发布时间】:2011-04-21 00:13:38
【问题描述】:

我问了两个相关的问题。

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

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

【问题讨论】:

    标签: c# dialog drawing console-application


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        假设你的意思是一个字符框,这就可以了。

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

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

        【讨论】:

        • 好点,乍一看,我认为我明白了,谢谢,但我需要更多地解决这个问题以获得可靠的解决方案,然后我会发布它
        猜你喜欢
        • 2011-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-15
        • 1970-01-01
        • 1970-01-01
        • 2010-10-25
        相关资源
        最近更新 更多