9

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?

Greg S
  • 12,333
  • 2
  • 41
  • 48
Pieces
  • 2,256
  • 5
  • 25
  • 39

4 Answers4

10

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;
}
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • I got this **SetConsoleScreenBufferSize() failed! Reason : 6** – Haseeb Mir Apr 17 '18 at 04:58
  • @HaSeeBMiR [Code 6](https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382) is for **ERROR_INVALID_HANDLE**. Your `hOut` is certainly not valid. – karlphillip May 11 '18 at 01:46
  • It seems the code provided produces an error. The tutorial linked in the provided forum is also inaccessible now. – Scene Jul 09 '21 at 04:18
  • It's a 10+ years old answer. Content on Stackoverflow is not supposed to be updated forever. Anyway, what is the error? – karlphillip Jul 09 '21 at 13:05
  • @karlphillip Quoting the user who posted the code: "The call to SetConsoleScreenBuffer fails, with error message "87"". I've posted a new answer which seems to work fine. – Scene Jul 10 '21 at 02:40
4

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

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
3

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

ShowScrollBar(GetConsoleWindow(), SB_VERT, 0);

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

x00
  • 13,643
  • 3
  • 16
  • 40
frisco
  • 27
  • 3
1

To remove scrollbars from the console, we can make the console screen buffer the same size as the console window. This can be done as follows:

#include <Windows.h>
#include <iostream>

int main() {
    CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo; 

    // Get console handle and get screen buffer information from that handle.
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsole, &screenBufferInfo);

    // Get rid of the scrollbar by setting the screen buffer size the same as 
    // the console window size.
    COORD new_screen_buffer_size;

    // screenBufferInfo.srWindow allows us to obtain the width and height info 
    // of the visible console in character cells.
    // That visible portion is what we want to set the screen buffer to, so that 
    // no scroll bars are needed to view the entire buffer.
    new_screen_buffer_size.X = screenBufferInfo.srWindow.Right - 
    screenBufferInfo.srWindow.Left + 1; // Columns
    new_screen_buffer_size.Y = screenBufferInfo.srWindow.Bottom - 
    screenBufferInfo.srWindow.Top + 1; // Rows

    // Set new buffer size
    SetConsoleScreenBufferSize(hConsole, new_screen_buffer_size);

    std::cout << "There are no scrollbars in this console!" << std::endl;

    return 0;
}
Scene
  • 489
  • 4
  • 16
  • The idea is correct. However, the code is not currently compilable because `hConsole` was not declared. There's also a comment that refers to `temp_screenBufferInfo` which is not defined on the code but I'm guessing you meant to say `screenBufferInfo`. – karlphillip Jul 10 '21 at 10:00
  • Thanks for the heads-up! I had used the code in another context. I've updated it. – Scene Jul 10 '21 at 15:21