2

I've created a simple console application which creates a 16 x 16 window and fills it with some characters.

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. But for some reason when I try to run the same .exe file from the folder console output gets corrupted.

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. 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.

enter image description here enter image description here

Also output gets even more corrupted after minimizing the window and displaying it again (and borders got vanished):

enter image description here

StrandedKitty
  • 305
  • 3
  • 14
  • Which .NET version are you used? I just tried on .NET Core 3.0 and it looks as expected using command prompt and powhershell. dont know if the .NET version can change anything. – TiGreX Feb 22 '20 at 21:13
  • @TiGreX .NET Core 3.1. I've just tried to compile it on 3.0, but it doesn't affect anything. Do you get empty black borders when running it on your machine? – StrandedKitty Feb 22 '20 at 21:17
  • now is happening to me even on visual stuido :/ the black border is for the scrollbar I assume. so what i think is: the CMD removes the scrollbar which converts into that black border, then when you minimize and bak up again, it take off the scroll bar space and it got added to the buffer. something like, that, defenitly werid behhaviour. I would reccomend to add an Issue on github :/ – TiGreX Feb 22 '20 at 21:33
  • @TiGreX Well, I've seen a few console games and it seems like people somehow manage to overcome these issues. – StrandedKitty Feb 22 '20 at 21:40
  • hummm insteresting, defenitly it has to be possible. I was thinking that maybe you can caputre the event of maximize/minimize and then set the window size and buffer again (but its not possible in cmd). I tried addin the `SetWindowSize` and `SetBufferSize` into the while, and it worked as expected, but, if you resize the window manually, it crashes. defenilty its an start and probably the solution is something like this. – TiGreX Feb 22 '20 at 21:50
  • @TiGreX Oh, updating window and buffer size in loop really helps. However, there is another problem: when running exe file the first row with 0s if hidden and when running in VS it is displayed and the cursor is at the end of 16th line. – StrandedKitty Feb 22 '20 at 21:59
  • do you need the cursor at all? you can always set it anywhere in the first few lines, like `SetCursorPossition(0,0)` and then just hide it with `CursorVisible = false` – TiGreX Feb 22 '20 at 22:07
  • @TiGreX I didn't hide it only to show that it moves to the next line and as a result the first line disappears. – StrandedKitty Feb 22 '20 at 22:09
  • Is there any difference if you replace the _heating statement_ (`while(true) { }`) with [`Console.ReadLine();`](https://learn.microsoft.com/en-us/dotnet/api/system.console.readline?view=netframework-4.8) so that the application doesn't exit until you mash the `Enter` key? – HABO Feb 23 '20 at 03:57
  • @HABO It's the same. – StrandedKitty Feb 23 '20 at 08:23

1 Answers1

0

I guess you cannot get rid of black borders in cmd. Try using Console.WriteLine() and print a whole row or just add Console.Write("\n") after your inner for loop. This will keep your formatting. But in this case you must increase your Buffer width to 17 (16 symbols + '\n').

Also I if your WindowSize is less than BufferSize you will get scrollbars instead of black borders.

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:

Code output

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.

  • Thanks for your answer, but as I stated in the comments borders can be removed by updating buffer and window size in the loop. And how slow output is? I'd like to be able to print at least 1000 characters with unique colors and backgrounds without glimmering. – StrandedKitty Feb 22 '20 at 22:34