c++ - 如何删除控制台窗口 C++ 中的滚动条

标签 c++ windows command-line console scrollbar

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

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

最佳答案

要删除滚动条,只需将屏幕缓冲区高度设置为与窗口高度相同:

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

关于c++ - 如何删除控制台窗口 C++ 中的滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3471520/

相关文章:

c++ - pimpl 习语和构建器模式之间有重叠吗?

c++ - 没有优先级且左结合的字符串计算器

windows - 在没有 GUI 或 msi 安装程序的情况下手动创建 msix 包

c++ - Windows Netapi32

c++ - 为什么 C++ 使用指针?

c++ - 在 GDI 中获取像素

powershell - 我可以通过powershell为tightVNC设置密码吗?

c++ - 从命令行编译 Visual Studio 项目中的特定对象?

java - 如果我希望它通过命令行将项目作为 jar 文件运行,某些属性文件的路径应该是什么样子

c++ - 使用程序在 C++ 中自动使用命令提示符