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

21

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

问题描述

我问了两个相关的问题.

I ask for 2 related questions.

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

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

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

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# 控制台应用程序中绘制方框、矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14