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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3471520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:01:47  来源:igfitidea点击:

How to remove scrollbars in console windows C++

c++windowscommand-lineconsolescrollbar

提问by Pieces

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.

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

How can I accomplish this same feature?

我怎样才能完成同样的功能?

采纳答案by karlphillip

These guysshow how to do it:

这些家伙展示了如何做到这一点:

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

回答by Hans Passant

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

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