简体   繁体   English

C#绘制框设置光标

[英]C# draw box set cursor

I am trying to draw a box in a console but I cant figure out to do it without entering a new line. 我正在尝试在控制台中画一个框,但我想不通而又不用输入新行。

I make something like this: 我做这样的事情:

-----
|   |
_____

But the input line comes directly underneath it and removing the upper row of the box. 但是输入线直接位于其下方,并删除了该框的上一行。

I tried with Console.setCursorPosition but the last line is already filled thus already a new line created. 我尝试了Console.setCursorPosition但是最后一行已经填满,因此已经创建了新行。

How can I prevent the new line from being created? 如何防止创建新行?

Thanks in advance 提前致谢

Edit It is possible to use the console window as a canvas? 编辑可以将控制台窗口用作画布吗? Lets assume a window with the following size 让我们假设一个窗口具有以下大小

Height : 50 Width : 100 高度:50宽度:100

I can place any character on any of the tiles within the above given console size. 我可以在上述指定控制台大小内的任何图块上放置任何字符。

Would this be possible if so how? 如果可以的话,这可能吗?

I think that very similair task have guys in Microsoft. 我认为,在Microsoft中,这是一个非常类似的任务。 Have a look: 看一看:

http://msdn.microsoft.com/en-us/library/system.console.setcursorposition.aspx There's something very similair. http://msdn.microsoft.com/zh-cn/library/system.console.setcursorposition.aspx有些东西很相似。 Hope this helps. 希望这可以帮助。

EDIT: 编辑:

Console.Write("\b\b");

This should remove last '\\r\\n' characters for new line. 这应删除换行符的最后一个'\\ r \\ n'字符。

It would be helpful if you posted code you wrote to create this box from ASCII chars. 如果您发布编写的代码以使用ASCII字符创建此框,这将很有帮助。

If you use Console.WriteLine() , just use Console.Write() method for the last line instead. 如果使用Console.WriteLine() ,则只需在最后一行使用Console.Write()方法即可。 So, for the box in your question the code can look like: 因此,对于您问题中的方框,代码可能如下所示:

...
Console.WriteLine("-----");
Console.WriteLine("|   |");
Console.Write("-----");
...

Let me know if this helps. 让我知道是否有帮助。

I made a test in a sample application and the extra line seems not to be created. 我在一个示例应用程序中进行了测试,似乎没有创建多余的行。

Update 更新资料

If you want to use the box a border to the Console window, I suppose it would be the easiest solution (if not the only one) not to add the last - character in the bottom line of the box you are drawing. 如果要使用该框作为“控制台”窗口的边框,我认为这是最简单的解决方案 (如果不是唯一的解决方案 ),而不是在要绘制的框的底行添加最后一个-字符。

Update II 更新二

You can try to use a simple trick and dynamically change height of the console window before drawing the last line. 您可以尝试使用一个简单的技巧,并在绘制最后一行之前动态更改控制台窗口的高度。 This will prevent the window from being scrolled. 这将防止滚动窗口。

Try code like this: 试试这样的代码:

int width = Console.WindowWidth;
int height = Console.WindowHeight;
int i, j;
for (i = 0; i < width; ++i)
{
    Console.Write("-");
}

for (j = 0; j < height - 2; ++j)
{
    Console.Write("|");

    for (i = 0; i < width - 2; ++i)
    {
        Console.Write(" ");
    }

    Console.Write("|");
}

//enlarge window in order to prevent it from being scrolled
Console.WindowHeight += 1;

for (i = 0; i < width; ++i)
{
    Console.Write("-");
}
//restore window's original size
Console.WindowHeight -= 1;
//set cursor inside the border
Console.SetCursorPosition(1, 1);

This should allow you to get the whole border visible in the window. 这样可以使整个边框在窗口中可见。

Update III 更新三

The suggested solution 建议的解决方案

Use the code above, but with Console.BufferWidth instead of Console.WindowHeight . 使用上面的代码,但使用Console.BufferWidth而不是Console.WindowHeight It seems to work correctly and does not require to modify window's dimensions. 它似乎可以正常工作,并且不需要修改窗口的尺寸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM