【问题标题】:C/C++ Remove Console Scrollbar on Win/Unix/MacOSC/C++ 删除 Win/Unix/MacOS 上的控制台滚动条
【发布时间】:2022-02-14 08:33:12
【问题描述】:

上下文:我正在编写一个小型库来将 ansi 格式的媒体输出到标准输出(类似于 ncurses)。 我想要做的是独立于编译代码的平台(Win / MacOS / Unix)删除终端窗口的滚动条。 下面是窗口应该是什么样子的一个小例子(这是用 ncurses 完成的):example of scrollbar missing

要使用 c++ 在 Windows 上移除滚动条,您只需将屏幕缓冲区高度设置为与窗口高度相同的大小,如下所示:

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

see here

我现在的问题是:我怎样才能删除其他平台上的滚动条并使用它们各自的方法与#if defined(_whatever plattform_) 类似我项目中的另一个代码sn-p(纯c):

// writes terminal dimensions to 'rows' and 'cols'
// returns 0 if successful
int window_dimensions()
{
#if defined(__APPLE__) || defined(__linux__)
    struct winsize w;

    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

    rows = w.ws_row;
    cols = w.ws_col;
    return 0;
#elif defined(_WIN32)
    CONSOLE_SCREEN_BUFFER_INFO csbi;

    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
    rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
    cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
    return 0;
#endif
    return 1;
}

【问题讨论】:

  • 您发布的是 C++ 代码,而不是 C。它们不是同一种语言。
  • @AndrewHenle 是的,我知道第一个例子是 c++,但问题更多是关于如何做。我很乐意接受任何一种语言的建议。
  • 然后编辑问题并添加C++标签。
  • 人们可以使用几十种不同的终端仿真器。适用于其中一个的东西可能不适用于其他人。我什至不会费心去尝试。
  • @Shawn 我不会为每个系统上的每个终端都准备过。 Win10、最近的 MacOS 和常见的 Linux(如 Ubuntu 20)就足够了。无论如何,我的程序不会在 termios 环境以外的任何东西上运行...

标签: c++ c unix terminal scrollbar


【解决方案1】:

对于将来发现这个问题并想知道的任何人,我认为这在我的小项目范围内是不可能的。

想要在 MacOS 上隐藏 Scrollbar 的用户可以前往 System Preferences -> General 并将 Show scroll bars 更改为 When scrolling

对于 Linux 用户,大多数终端上的栏甚至没有那么大的不便,所以你应该能够忍受它。

在 Windows 机器上,我在c 中采用了这种方法:

int remove_scrollbar(int x_, int y_)
{
#if defined(_WIN32)
    CONSOLE_SCREEN_BUFFER_INFOEX csbiex;

    csbiex.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);

    GetConsoleScreenBufferInfoEx(GetStdHandle(STD_OUTPUT_HANDLE), &csbiex);

    csbiex.dwSize.X = x_;
    csbiex.dwSize.Y = y_;

    SMALL_RECT s1;
    s1.Top = 0;
    s1.Left = 0;
    s1.Bottom = y_;
    s1.Right = x_;

    csbiex.srWindow = s1;

    COORD mws1;
    mws1.X = x_;
    mws1.Y = y_;

    csbiex.dwMaximumWindowSize = mws1;

    SetConsoleScreenBufferInfoEx(GetStdHandle(STD_OUTPUT_HANDLE), &csbiex); //this also resizes the window to x_, y_
    return 0;
#endif
    return 1;
}

我选择了CONSOLE_SCREEN_BUFFER_INFOEX,因为它在我的测试中比CONSOLE_SCREEN_BUFFER_INFO 效果更好,但两者都应该没问题。

【讨论】:

    猜你喜欢
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    相关资源
    最近更新 更多