Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove scrollbars in console windows C++

I have been checking out some Rogue like games (Larn, Rogue, etc) that are written in C and C++, and I have noticed that they do not have the scrollbars to the right of the console window.

How can I accomplish this same feature?

like image 829
Pieces Avatar asked Aug 12 '10 19:08

Pieces


3 Answers

To remove the scrollbar, simply set the screen buffer height to be the same size as the height of the window:

#include <windows.h>
#include <iostream>
using namespace std;

int main()
{                
    // get handle to the console window
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    // retrieve screen buffer info
    CONSOLE_SCREEN_BUFFER_INFO scrBufferInfo;
    GetConsoleScreenBufferInfo(hOut, &scrBufferInfo);

    // current window size
    short winWidth = scrBufferInfo.srWindow.Right - scrBufferInfo.srWindow.Left + 1;
    short winHeight = scrBufferInfo.srWindow.Bottom - scrBufferInfo.srWindow.Top + 1;

    // current screen buffer size
    short scrBufferWidth = scrBufferInfo.dwSize.X;
    short scrBufferHeight = scrBufferInfo.dwSize.Y;        

    // to remove the scrollbar, make sure the window height matches the screen buffer height
    COORD newSize;
    newSize.X = scrBufferWidth;
    newSize.Y = winHeight;

    // set the new screen buffer dimensions
    int Status = SetConsoleScreenBufferSize(hOut, newSize);
    if (Status == 0)
    {
        cout << "SetConsoleScreenBufferSize() failed! Reason : " << GetLastError() << endl;
        exit(Status);
    }

    // print the current screen buffer dimensions
    GetConsoleScreenBufferInfo(hOut, &scrBufferInfo);
    cout << "Screen Buffer Size : " << scrBufferInfo.dwSize.X << " x " << scrBufferInfo.dwSize.Y << endl;

    return 0;
}
like image 156
karlphillip Avatar answered Sep 18 '22 12:09

karlphillip


You need to make the console screen buffer the same size as the console window. Get the window size with GetConsoleScreenBufferInfo, srWindow member. Set the buffer size with SetConsoleScreenBufferSize().

like image 20
Hans Passant Avatar answered Sep 19 '22 12:09

Hans Passant


Using #include <winuser.h>, you can simply do

ShowScrollBar(GetConsoleWindow(), SB_VERT, 0);

You can specify which scroll bar to hide using different parameters.

like image 32
frisco Avatar answered Sep 16 '22 12:09

frisco