ncures 中的颜色对在类中无法正常工作
Color pairs in ncurses not working properly in classes
我正在用ncurses制作一个基本的终端应用程序引擎。我创建了一个基本的窗口类,它存储您正在使用的窗口,并允许您在其上编写。现在,我正在做一个 ColorWindow 类,它继承自 BasicWindow,应该允许你用颜色编写。根据抽象,我正在初始化类的方法之一中所需的颜色对,这是示例代码:
编辑:我在这里写所有代码
#include <ncurses.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <iomanip>
#include <locale>
#include <sstream>
#include <string>
class ColorWindow : public BasicWindow {
private:
int npairs;
bool canColor;
void initializePairs() {
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_YELLOW, COLOR_BLACK);
init_pair(4, COLOR_BLUE, COLOR_BLACK);
init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
init_pair(6, COLOR_CYAN, COLOR_BLACK);
init_pair(7, COLOR_WHITE, COLOR_BLACK);
npairs = 8;
}
public:
ColorWindow() {
if(has_colors()) {
initializePairs();
this->canColor = true;
}
else {
this->canColor = false;
}
}
ColorWindow(int he, int wi, int stx, int sty) : BasicWindow(he, wi, stx, sty) {
if(has_colors()) {
initializePairs();
this->canColor = true;
}
else {
this->canColor = false;
}
}
int addColorPair(int foreground, int background) {
if(foreground < 0 || foreground > 7
|| background < 0 || background > 7)
return -1;
init_pair(npairs, foreground, background);
npairs++;
return npairs-1;
}
bool ColorWindow::writeStringWithColor(int x, int y,const char* message, int pair) {
if(!isInLimits(x, y))
return false;
if(pair >= npairs)
return false;
if(canColor)
return false;
wattron(getContainer(), COLOR_PAIR(pair));
mvwprintw(getContainer(), x, y, message);
wattroff(getContainer(), COLOR_PAIR(7));
}
bool changeColor(int color, int r, int g, int b) {
if(!can_change_color())
return false;
if(color < 0 || color > 7 || r < 0 || r > 1000
|| g < 0 || g > 1000 || b < 0 || b > 1000)
return false;
init_color(color, r, g, b);
return true;
}
};
class BasicWindow {
private:
WINDOW* container;
int startx, starty, height, width;
public:
BasicWindow() {
this->height = 20;
this->width = 84;
this->starty = (LINES - height) / 2; /* Calculating for a center placement */
this->startx = (COLS - width) / 2;
}
BasicWindow(int he, int wi, int stx, int sty) {
this->height = he;
this->width = wi;
this->starty = sty;
this->startx = stx;
}
WINDOW* createNewContainer() {
this->container = newwin(height, width, starty, startx);
wrefresh(this->container); /* Show that box */
return getContainer();
}
WINDOW* getContainer() {
return this->container;
}
bool writeString(int x, int y, const char* message) {
if(!isInLimits(x, y))
return false;
mvwprintw(this->container, x, y, message);
}
void refreshContainer() {
refresh();
wrefresh(this->container); /* Show that box */
}
void destroyWindow() {
wborder(this->container, ' ', ' ', ' ',' ',' ',' ',' ',' ');
wrefresh(this->container);
delwin(this->container);
}
bool isInLimits(int x, int y) {
if(x < 0 || x >= this->width-1) {
return false;
}
if(y < 0 || y >= this->height-1) {
return false;
}
return true;
};
这是主要的:
#include <iostream>
#include <unistd.h>
#include <ncurses.h>
#include "windows.h"
int main() {
ColorWindow cw = ColorWindow(20, 80, 0, 0);
initscr(); /* Start curses mode */
cbreak(); /* Line buffering disabled, Pass on
* everty thing to me */
keypad(stdscr, TRUE);
start_color();
WINDOW* container;
container = cw.createNewContainer();
cw.writeStringWithColor(0, 10, "Hello everyone in color!!", 2);
cw.refreshContainer();
sleep(2);
endwin();
return 0;
}
它初始化颜色模式,但不显示任何内容,就好像选择的对是黑黑色一样。
运行代码(并使用 ncurses 的调试跟踪功能对其进行跟踪),只有一次调用 init_pair
,使用似乎来自未初始化值的对号,例如,7978
在列表的这一部分中:
called {init_pair(0x193e250,7978,5,7)
return }0
对源的快速扫描显示对应于此方法:
/* PUBLIC METHODS */
int ColorWindow::addColorPair(int foreground, int background) {
if(foreground < 0 || foreground > 7
|| background < 0 || background > 7)
return -1;
init_pair(npairs, foreground, background);
npairs++;
return npairs-1;
}
并且npairs
在构造函数中永远不会设置为零。
您可能会发现valgrind
对于查看此类问题很有用。
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- C++程序已停止工作-求解常微分方程