如何在控制台窗口C ++中删除滚动条

时间:2010-08-12 19:54:48

标签: c++ windows command-line console scrollbar

我一直在检查用C和C ++编写的一些Rogue like games(Larn,Rogue等),我注意到它们没有控制台窗口右侧的滚动条。

我如何完成同样的功能?

4 个答案:

答案 0 :(得分:8)

These guys显示如何操作:

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

int main()
{
    HANDLE hOut;
    CONSOLE_SCREEN_BUFFER_INFO SBInfo;
    COORD NewSBSize;
    int Status;

    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    GetConsoleScreenBufferInfo(hOut, &SBInfo);
    NewSBSize.X = SBInfo.dwSize.X - 2;
    NewSBSize.Y = SBInfo.dwSize.Y;

    Status = SetConsoleScreenBufferSize(hOut, NewSBSize);
    if (Status == 0)
    {
        Status = GetLastError();
        cout << "SetConsoleScreenBufferSize() failed! Reason : " << Status << endl;
        exit(Status);
    }

    GetConsoleScreenBufferInfo(hOut, &SBInfo);

    cout << "Screen Buffer Size : ";
    cout << SBInfo.dwSize.X << " x ";
    cout << SBInfo.dwSize.Y << endl;

    return 0;
}

答案 1 :(得分:4)

您需要使控制台屏幕缓冲区与控制台窗口的大小相同。使用GetConsoleScreenBufferInfo,srWindow成员获取窗口大小。使用SetConsoleScreenBufferSize()设置缓冲区大小。

答案 2 :(得分:1)

要从控制台移除滚动条,我们可以使控制台屏幕缓冲区与控制台窗口的大小相同。这可以按如下方式完成:

#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;
}

答案 3 :(得分:0)

使用#include <winuser.h>,你可以简单地做

ShowScrollBar(GetConsoleWindow(), SB_VERT, 0);

您可以使用不同的参数specify隐藏滚动条。