我正在尝试将此功能(用FreeBasic编写)移植到C ++。以下代码将在控制台窗口的边缘周围打印一个字符边框:
#Define HORIZONTAL_SYMBOL 205
#Define VERTICAL_SYMBOL 186
#Define TOP_LEFT_CORNER 201
#Define TOP_RIGHT_CORNER 187
#Define BOTTOM_LEFT_CORNER 200
#Define BOTTOM_RIGHT_CORNER 188
Sub print_frame(ByVal max_column As Short = 80, ByVal max_row As Short = 25, ByVal start_x As Short = 1, ByVal start_y As Short = 1)
Dim As Byte cycles
'Print top line
Locate(start_y,start_x)
? Tab(start_x); Chr(TOP_LEFT_CORNER); String(max_column-2, Chr(HORIZONTAL_SYMBOL)); Chr(TOP_RIGHT_CORNER);
'Body
Do
cycles += 1
? Tab(start_x); Chr(VERTICAL_SYMBOL); Tab(max_column+start_x-1);
Chr(VERTICAL_SYMBOL);
Loop Until cycles = max_row-2
'Print bottom line
? Tab(start_x); Chr(BOTTOM_LEFT_CORNER); String(max_column-2,
Chr(HORIZONTAL_SYMBOL)); Chr(BOTTOM_RIGHT_CORNER);
End Sub
Screen 0
print_frame()
GetKey
请注意Screen 0
操作,它将启动80列/ 25行的Windows控制台模式(有些东西我还没有发现C ++中的等效项/替代项)。要手动设置此模式,请启动Windows Shell,右键单击程序栏中的程序图标,然后选择“属性”(希望这样,我的Windows安装程序是德语)。在新窗口中,选择“布局”标签,然后将缓冲区大小和窗口大小均设置为80x25。
请忽略#define
行,该程序在Linux上使用differentnet符号。
这是我试图将此函数移植到C ++的尝试:
#include "rlutil.h"
void print_frame(short int max_column = rlutil::tcols(), short int max_row = rlutil::trows(), short int x = 1, short int y = 1) {
short int cycles = 0;
// Print top line
rlutil::locate(x,y);
std::cout << char (201) << std::string(max_column-2, 205) << char (187);
// Body
while (cycles < max_row-2) {
cycles++;
rlutil::locate(x,y+cycles);
std::cout << char (186) << std::string(max_column-2, 0) << char (186);
}
// Print bottom line
rlutil::locate(x,y+max_row-1);
std::cout << char (200) << std::string(max_column-2, 205) << char (188);
}
int main () {
print_frame();
}
上面的代码使用了一个名为rlutil::locate()
的函数,它不是由我编写的,也不是一个名为“ rlutils.h”的库的一部分(如果需要,可以用google搜索)。
其他库函数rlutil::tcols()
和rlutil::trows()
返回控制台窗口的行数和列数。
我的问题是,尽管FreeBasic函数不会向下滚动文本(这会导致顶部边界线被切除),但C ++代码却可以。我如何避免这种行为?
编辑:我找到了一种防止使用putchar()
滚动的方法(用std::cout
打印除最后一个字符以外的所有字符,并使用putchar()
放置最后一个字符),但我仍在搜索更加聪明的方法。