简体   繁体   English

奇怪的 Console.Write() 行为

[英]Strange Console.Write() behaviour

I've created a simple console application which creates a 16 x 16 window and fills it with some characters.我创建了一个简单的控制台应用程序,它创建了一个 16 x 16 的窗口并用一些字符填充它。

The problem is that it runs fine (and according to expectations) only when I launch it in Microsoft Visual Studio 2019. It doesn't matter if I use Debug or Release mode.问题是它只有在我在 Microsoft Visual Studio 2019 中启动时才能正常运行(并且符合预期)。我使用调试模式还是发布模式都没有关系。 But for some reason when I try to run the same .exe file from the folder console output gets corrupted.但是由于某种原因,当我尝试从文件夹控制台输出中运行相同的 .exe 文件时,该文件已损坏。

Furthermore, there are strange borders on the right and bottom of the window.此外,窗口的右侧和底部有奇怪的边框。 I guess this space may be reserved for scroll bars, but sometimes when I run my app in Visual Studio no borders appear.我想这个空间可能是为滚动条保留的,但有时当我在 Visual Studio 中运行我的应用程序时,不会出现边框。 Eventually I would like to make a console game, so these borders which seem to appear randomly will be a problem.最终我想制作一个控制台游戏,所以这些似乎随机出现的边框将是一个问题。

My code:我的代码:

using System;

namespace App
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.SetWindowSize(16, 16);
            Console.SetBufferSize(16, 16);

            //Console.CursorVisible = false;

            for(int x = 0; x < 16; x++)
                for (int y = 0; y < 16; y++)
                    Console.Write(x.ToString("X"));

            while(true) { } // prevents window from closing instantly
        }
    }
}

Correct result when running from Visual Studio is on the left, incorrect result when running the same exe file from output folder is on the right.从 Visual Studio 运行时的正确结果在左侧,从输出文件夹运行相同的 exe 文件时的错误结果在右侧。

在此处输入图片说明在此处输入图片说明

Also output gets even more corrupted after minimizing the window and displaying it again (and borders got vanished):在最小化窗口并再次显示后,输出也会变得更加损坏(并且边框消失了):

在此处输入图片说明

I guess you cannot get rid of black borders in cmd.我猜你无法摆脱 cmd 中的黑色边框。 Try using Console.WriteLine() and print a whole row or just add Console.Write("\\n") after your inner for loop.尝试使用 Console.WriteLine() 并打印一整行,或者在内部 for 循环之后添加 Console.Write("\\n") 。 This will keep your formatting.这将保留您的格式。 But in this case you must increase your Buffer width to 17 (16 symbols + '\\n').但在这种情况下,您必须将缓冲区宽度增加到 17(16 个符号 + '\\n')。

Also I if your WindowSize is less than BufferSize you will get scrollbars instead of black borders.另外,如果您的 WindowSize 小于 BufferSize,您将获得滚动条而不是黑色边框。

I have modified your code a bit:我稍微修改了你的代码:

        Console.SetBufferSize(17, 17);

        //Console.CursorVisible = false;

        for (int x = 0; x < 16; x++)
        {
            for (int y = 0; y < 16; y++)
                Console.Write(x.ToString("X"));
            Console.Write("\n");
        }

Code creates output like this:代码创建这样的输出:

代码输出

Minimizing the window causes console scrolling.最小化窗口会导致控制台滚动。 You can get rid of this by increasing your window size or by implementing a rendering loop as it clears the console and moves the pointer to the top.您可以通过增加窗口大小或在清除控制台并将指针移动到顶部时实现渲染循环来摆脱这种情况。 C# console output is very slow so unless you make it faster your text will glimmer all the time. C# 控制台输出非常慢,所以除非你让它更快,否则你的文本会一直闪烁。

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

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