所以,我使用pdcurses來添加一些顏色到我的控制檯應用程序,但我遇到了問題。如果我製作第二個窗口並嘗試着色它的輸出,它工作得很好,但如果我嘗試將輸出顏色爲stdscr,則什麼都不會發生。爲什麼不能用於stdscr的顏色? (PDCurses)
我想繼續使用stdscr而不是用另一個窗口來覆蓋它,因爲stdscr會收到我通常發送給標準輸出的輸出,允許我使用C++風格的界面到控制檯。通過發送輸出到cout,它發送到stdscr,並且這是目前我知道使用C++接口訪問pdcurses的唯一方法。另外,其他庫偶爾會將它們的輸出直接發送到stdout,如果我使用stdscr,則輸出不會丟失(我知道lua的print
函數不在我聽說的頂部)。
下面是一些示例代碼:
// This prints a red '>' in the inputLine window properly. //
wattron(inputLine, A_BOLD | COLOR_PAIR(COLOR_RED));
wprintw(inputLine, "\n> ");
wattroff(inputLine, A_BOLD | COLOR_PAIR(COLOR_RED));
// This prints a light grey "Le Testing." to stdscr. Why isn't it red? //
wattron(stdscr, A_BOLD | COLOR_PAIR(COLOR_RED));
cout << "\nLe Testing.\n";
wattroff(stdscr, A_BOLD | COLOR_PAIR(COLOR_RED));
// This does nothing. I have no idea why. //
wattron(stdscr, A_BOLD | COLOR_PAIR(COLOR_RED));
wprintw(stdscr, "\nLe Testing.\n");
wattroff(stdscr, A_BOLD | COLOR_PAIR(COLOR_RED));
這是我如何初始化pdcurses:
// Open the output log which will mimic stdout. //
if (userPath)
{
string filename = string(userPath) + LOG_FILENAME;
log.open(filename.c_str());
}
// Initialize the pdCurses screen. //
initscr();
// Resize the stdout screen and create a line for input. //
resize_window(stdscr, LINES - 1, COLS);
inputLine = newwin(1, COLS, LINES - 1, 0);
// Initialize colors. //
if (has_colors())
{
start_color();
for (int i = 1; i <= COLOR_WHITE; ++i)
{
init_pair(i, i, COLOR_BLACK);
}
}
else
{
cout << "Terminal cannot print colors.\n";
if (log.is_open())
log << "Terminal cannot print colors.\n";
}
scrollok(stdscr, true);
scrollok(inputLine, true);
leaveok(stdscr, true);
leaveok(inputLine, true);
nodelay(inputLine, true);
cbreak();
noecho();
keypad(inputLine, true);
我在做什麼錯?
確實這是一個錯字,謝謝。按照預期,使用printw和wprintw可以打印彩色文本。有一件事讓我困惑,即'printw'的輸出出現在'cout'的上面,即使'printw'後來出現在代碼中也是如此。我想這只是標準輸出,像你說的那樣令人困惑。我可以修改我的代碼,純粹使用pdcurses api(至少在這個階段)。現在我正在使用'std :: ostringstream流;流<<數據; (stream.str()c_str())printw;'。 – 2014-09-21 05:35:23