Search code examples
c++visual-studiowindowoutputwhitespace

VS2019 Output Window Comes with lot of whitespace


I don't think this question has been asked before, but here it is:

When I run my program in VS2019, the output window executes properly, depending on errors.

But this time I am creating a program, which depends on the fullscreen console output window columns and rows length.

But I've noticed that the number of columns is more than rows, because of the additional whitespace which comes with the output window.

I've modified my code as a sample:

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;

int main() {
    SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE), CONSOLE_FULLSCREEN_MODE, 0);
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    int WindowColumns, WindowRows, DisplayBuffer, DisplayColumns, DisplayRows;
    string DisplayMap;
    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
    WindowColumns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
    WindowRows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
    
    return 0;
}

Here's the output:

237,67

The above output displays the Columns first, and rows. I am sure about that. The number of columns is more than the number of rows, which should be opposite, considering my screen resolution is 1920x1080

The screenshot which shows the amount of whitespace generated in the output:

I dont have enough reputation to embed the image, so here's a link

My question is: How do I get rid of this additional whitespace, it should only match the length of rows and columns. Can you help me solve this?

Thanks in advance.


Solution

  • I suggest you could try to use SMALL_RECT structure to specified console screen buffer.

    I suggest you could follow the following code:

    #include <stdlib.h>
    #include <stdio.h>
    #include <iostream>
    #include <Windows.h>
    #include <string>
    using namespace std;
    
    int main() {
        SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE), CONSOLE_FULLSCREEN_MODE, 0);
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        int WindowColumns, WindowRows, DisplayBuffer, DisplayColumns, DisplayRows;
        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
        SMALL_RECT rect = csbi.srWindow;
        COORD size = { rect.Right + 1,rect.Bottom + 1 };
        SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), size);//Changes the size of the specified console screen buffer.
        WindowColumns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
        WindowRows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
    
        return 0;
    }