代码之家  ›  专栏  ›  技术社区  ›  siamak

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

  •  3
  • siamak  · 技术社区  · 15 年前

    我问了2个相关问题。

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

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

    3 回复  |  直到 10 年前
        1
  •  4
  •   rerun    15 年前

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

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

    我编辑了代码,把一条消息放进他们的。你需要在边界条件上做更多的工作。例如,邮件中没有空格,一个单词比框长,但这应该足以让您开始阅读。

        2
  •  2
  •   ChristopheD    15 年前

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

        3
  •  0
  •   Jonathan    15 年前

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