【发布时间】: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;
}
我现在的问题是:我怎样才能删除其他平台上的滚动条并使用它们各自的方法与#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