简体   繁体   English

在 Linux 上使用 ncurses 的背景颜色

[英]Background color using ncurses on Linux

first post here so I apologize for any errors.第一次在这里发帖,所以我为任何错误道歉。

Basically, when I run my code, everything works properly except for changing the background color.基本上,当我运行我的代码时,除了更改背景颜色外,一切正常。 For some reason it is always grey.出于某种原因,它总是灰色的。 I'm attempting to change it to black but it's not working and I'm not sure why.我正在尝试将其更改为黑色,但它不起作用,我不确定为什么。

The main part of my code that I believe should be changing the background color is this: wattron(mainWindow, COLOR_BLACK);我认为应该更改背景颜色的代码的主要部分是: wattron(mainWindow, COLOR_BLACK);

Any help to figure out how to change my background to Black would be greatly appreciated.任何帮助弄清楚如何将我的背景更改为黑色的帮助将不胜感激。 Thanks guys!谢谢你们!

Here is what I have so far in case it helps give some context for my problem:这是我到目前为止所拥有的,以防它有助于为我的问题提供一些背景:

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

In the call wattron(WINDOW* win, chtype ch) you cannot use COLOR_BLACK as ch because COLOR_BLACK is an integer, not an attribute (value of chtype ).在调用wattron(WINDOW* win, chtype ch)您不能将 COLOR_BLACK 用作ch因为 COLOR_BLACK 是一个整数,而不是一个属性( chtype值)。 You should call this function using COLOR_PAIR :您应该使用COLOR_PAIR调用此函数:

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

Note that wattron only enables specified attributes.请注意, wattron仅启用指定的属性。 If you want to set a background (it's reset attributes of each cell) of a window, you will better use wbkgd(WINDOW *win, chtype ch) .如果要设置窗口的背景(每个单元格的重置属性),最好使用wbkgd(WINDOW *win, chtype ch) Documentation of it you can find in the wbkgd(3ncurses) manual page .您可以在wbkgd(3ncurses) 手册页中找到它的文档。

The ncurses' tutorial that helped me the most is here .对我帮助最大的 ncurses 教程在这里 I recommend you reading the 10th Colors section.我建议您阅读第 10 种颜色部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM