【问题标题】:Background color using ncurses on Linux在 Linux 上使用 ncurses 的背景颜色
【发布时间】:2014-12-09 21:40:31
【问题描述】:

第一次在这里发帖,如有错误,我深表歉意。

基本上,当我运行我的代码时,除了更改背景颜色外,一切正常。由于某种原因,它总是灰色的。我正在尝试将其更改为黑色,但它不起作用,我不知道为什么。

我认为应该更改背景颜色的代码的主要部分是: wattron(mainWindow, COLOR_BLACK);

任何帮助弄清楚如何将我的背景更改为黑色将不胜感激。谢谢大家!

这是我到目前为止所拥有的,以防它有助于为我的问题提供一些背景信息:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <curses.h>
#include <time.h>

// Variables
int nlines;
int ncols;
int x;
int y;
int y0;
int x0;
int input;

// Constants
const int MAX_LINES = 10;
const int MAX_COLUMNS = 10;

// Main function
int main(void) {
    WINDOW * mainWindow;

    // Initialize ncurses
    if ( (mainWindow = initscr()) == NULL) {
        fprintf(stderr, "Could not initialize ncurses!\n");
        exit(EXIT_FAILURE);
    }

    // Call function to use color
    start_color();

    // Create my own color pairs
    init_pair(1, COLOR_CYAN, COLOR_BLACK);
    init_pair(2, COLOR_BLUE, COLOR_RED);

    // First clear off the screen
    clear();

    // Move the cursor
    y = 8;
    x = 30;
    move(y, x);

    // Refresh
    refresh();

    // Test output - working
    // printw("Testing...");

    waddch(mainWindow, 'T' | A_UNDERLINE | COLOR_PAIR(1));
    waddch(mainWindow, 'E' | A_UNDERLINE | COLOR_PAIR(2));
    waddch(mainWindow, 'S' | A_UNDERLINE | COLOR_PAIR(1));
    waddch(mainWindow, 'T' | A_UNDERLINE | COLOR_PAIR(2));
    waddch(mainWindow, 'I' | A_UNDERLINE | COLOR_PAIR(1));
    waddch(mainWindow, 'N' | A_UNDERLINE | COLOR_PAIR(2));
    waddch(mainWindow, 'G' | A_UNDERLINE | COLOR_PAIR(1));
    waddch(mainWindow, '.' | A_UNDERLINE | COLOR_PAIR(2));
    waddch(mainWindow, '.' | A_UNDERLINE | COLOR_PAIR(1));
    waddch(mainWindow, '.' | A_UNDERLINE | COLOR_PAIR(1));
    waddch(mainWindow, '.' | A_UNDERLINE | COLOR_PAIR(2));

    // Make background a different color
    wattron(mainWindow, COLOR_BLACK);

    // Hold until user inputs a character
    input =  getch();

    // Clean up
    delwin(mainWindow);
    endwin();
    refresh();

    return EXIT_SUCCESS;
}

【问题讨论】:

    标签: c++ linux ncurses


    【解决方案1】:

    【讨论】:

    • 感谢您的评论。我尝试使用 wbkgd(WINDOW *win, chtype ch) 或在我的情况下使用 wbkgd(mainWindow, COLOR_BLACK) ,甚至确保在之后执行 refresh() 但它不起作用。背景仍然是灰色的。我试图调用 use_default_colors() 然后做所有事情,它将背景从灰色更改为正常的紫色,但实际上从未将其更改为黑色。我很抱歉,但我还是有点困惑:/
    • 原来灰色对我来说是黑色的......奇怪但任何其他颜色都像你说的那样完美。我的黑色一定有点偏,所以我会调整一下,它应该很好。谢谢!
    【解决方案2】:

    在调用wattron(WINDOW* win, chtype ch) 中,您不能将COLOR_BLACK 用作ch,因为COLOR_BLACK 是一个整数,而不是一个属性(chtype 的值)。你应该使用COLOR_PAIR调用这个函数:

    wattron(mainWindow, COLOR_PAIR(0)); // The pair number 0 is the terminal's default pair (white foreground, black background). 
    

    请注意,wattron 仅启用指定的属性。如果你想设置一个窗口的背景(它是每个单元格的重置属性),你最好使用wbkgd(WINDOW *win, chtype ch)。您可以在wbkgd(3ncurses) manual page 中找到它的文档。

    对我帮助最大的 ncurses 教程是 here。我建议您阅读第 10 种颜色部分。

    【讨论】:

      猜你喜欢
      • 2012-11-26
      • 1970-01-01
      • 2011-03-08
      • 2021-10-27
      • 2015-08-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      • 2014-02-04
      相关资源
      最近更新 更多