使用 ncurses 库创建文本用户界面:基础函数详解
简介
ncurses
是一个功能强大的库,用于在 Unix-like 系统中创建文本用户界面。它提供了丰富的函数来控制屏幕上的文本显示、处理键盘输入、绘制图形元素等。本文将详细介绍 ncurses
库中的一些基础函数,包括 printw
、wrefresh
、获取用户信息、键盘输入、绘制图形元素以及获取终端信息等。
初始化和清理
在使用 ncurses
函数之前,需要初始化 ncurses
环境,并在程序结束时清理环境。
#include <ncurses.h>
int main() {
initscr(); // 初始化 ncurses 环境
// ... 其他代码 ...
endwin(); // 结束 ncurses 环境
return 0;
}
printw
函数
printw
函数用于在当前光标位置打印文本。如果需要在特定位置打印文本,可以使用 mvprintw
函数。
printw("This is some text"); // 在当前光标位置打印文本
mvprintw(5, 10, "This is on row 5, column 10"); // 在第5行第10列打印文本
wrefresh
函数
wrefresh
函数用于将窗口的内容刷新到屏幕上。在 ncurses
中,所有输出都是先写入缓冲区,然后通过调用 wrefresh
将缓冲区的内容显示到屏幕上。
WINDOW *win;
win = newwin(10, 20, 5, 5); // 创建一个窗口
wprintw(win, "Hello World"); // 在窗口中打印文本
wrefresh(win); // 刷新窗口,显示文本
获取用户信息和键盘输入
getch
函数用于获取用户的键盘输入。
int ch = getch(); // 获取用户按键的 ASCII 码
绘制图形元素
box
函数用于在窗口中绘制一个矩形边框,mvprintw
可以在矩形内打印文本。
box(win, 0, 0); // 在窗口中绘制边框
wprintw(win, 1, 1, "This is inside the box"); // 在边框内打印文本
wrefresh(win); // 刷新窗口,显示内容
获取终端信息
getyx
、getbegyx
和 getmaxyx
函数分别用于获取当前光标位置、窗口的起始坐标和最大坐标。
int y, x, yBeg, xBeg, yMax, xMax;
getyx(stdscr, y, x); // 获取光标位置
getbegyx(stdscr, yBeg, xBeg); // 获取窗口起始坐标
getmaxyx(stdscr, yMax, xMax); // 获取窗口最大坐标
文本颜色和属性
ncurses
支持文本颜色和属性。attron
和 attroff
函数用于设置文本属性,如加粗、反色等。
attron(COLOR_PAIR(1) | A_BOLD, NULL, NULL); // 设置文本颜色和加粗
printw("This is bold and colored"); // 打印文本
attroff(COLOR_PAIR(1) | A_BOLD, NULL, NULL); // 关闭文本属性
wattron
是 ncurses
库中的一个函数,用于打开(启用)窗口(WINDOW
)的文本属性。ncurses
提供了多种文本属性,可以改变文本的外观,例如颜色、是否加粗、是否反色显示等。
函数原型如下:
int wattron(WINDOW *win, attr_t attr, void *opt);
-
win
:指定要设置属性的窗口。 -
attr
:指定要启用的属性。 -
opt
:指定颜色对(如果设置了颜色属性)。
例如,以下代码片段启用了窗口中文本的反色属性:
WINDOW *mywin;
mywin = newwin(10, 20, 5, 5); // 创建一个窗口
wattron(mywin, A_REVERSE); // 启用反色属性
printw(mywin, "This text is highlighted");
wrefresh(mywin); // 刷新窗口以显示更改
wattron
函数通常与 wattroff
函数一起使用,以便在需要时关闭(禁用)特定的文本属性。例如:
wattroff(mywin, A_REVERSE); // 关闭反色属性
ncurses
库提供了多种属性,包括:
A_STANDOUT
:文本加粗。
A_BLINK
:文本闪烁。
A_DIM
:文本变暗。
A_BOLD
:文本加粗。
A_PROTECT
:文本有边框保护。
A_INVIS
:文本可被覆盖。
A_ALTCHARSET
:使用备用字符集。
A_CHARTEXT
:文本有额外属性。
#include <ncurses.h>
#include <string.h>
int main()
{
initscr();
noecho();
cbreak();
int yMax, xMax;
getmaxyx(stdscr, yMax, xMax); // get the maximum y and x coordinates of the screen
WINDOW *menuwin = newwin(6, xMax - 12, yMax - 8, 5); // create a new window for the menuwin
box(menuwin, 0, 0); // draw a box around the menu
refresh(); // refresh the screen to show the changes
wrefresh(menuwin); // refresh the menu window
// makes it so we can use arrow keys
keypad(menuwin, true);
char choices[][20] = {"walk", "jog", "run"}; // array of strings for the menu choices
int choice; // variable to store the user/s choice
int highlight = 0; // variable to track the highlight menu choice
while (1) // main loop to handle user input
{
for (int i = 0; i < 3; i++) // loop through each menu choice
{
if (i == highlight) // if the current choice is highlighted
wattron(menuwin, A_REVERSE); // turn on reverse video attribute
mvwprintw(menuwin, i + 1, 1, choices[i]); // print the menu choice
wattroff(menuwin, A_REVERSE); // get the user's choice from the menu window
}
choice = wgetch(menuwin); // get the user's choice from the menu window
switch (choice) // switch based on the user's choice
{
case KEY_UP:
highlight--;
if (highlight = -1)
highlight = 0;
break;
case KEY_DOWN:
highlight++;
if (highlight == 3)
highlight = 2;
break;
default:
break;
}
if (choice == 10) // if the user pressed enter key
break; // exit the loop
}
printw("Your choice was :%s", choices[highlight]); // print the user's choice
getch(); // wait for the user to press any key before exiting
endwin(); // end the ncurses mode and restore the therminal settings
return 0; // return 0 to indicate successful program termination
}
#include <ncurses.h>
#include <stdlib.h>
// #include <time.h>
// #include <unistd.h>
// #include <stdio.h>
// #include <stdlib.h>
// #include <stdbool.h>
// #include <string.h>
// #include <stdbool.h>
/*
*A_NORMAL:默认属性,不加粗、不反色、不闪烁。
*A_STANDOUT:文本加粗。
*A_REVERSE:文本反色显示。
*A_BLINK:文本闪烁。
*A_DIM:文本半透明。
*A_BOLD:文本加粗。
*A_PROTECT:文本有边框保护,防止被覆盖。
*A_INVIS:文本可被覆盖。
*A_ALTCHARSET:使用备用字符集。
*A_CHARTEXT:测试字符。
*/
/*
*COLOR_PAIR(n)
*COLOR_BLACK 0
*COLOR_RED 1
*COLOR_GREEN 2
*COLOR_YELLOW 3
*COLOR_BLUE 4
*COLOR_MAGENTA 5
*COLOR_CYAN 6
*COLOR_WHITE 7
*/
int main()
{
initscr();
// start_ncurses(true, true);
start_color();
init_pair(1, COLOR_MAGENTA, COLOR_BLUE);
printw("This is normal test\n");
refresh();
attron(A_BOLD);
printw("This is some text\n");
attroff(A_BOLD);
attron(COLOR_PAIR(1));
printw("COLOR\n");
attron(COLOR_PAIR(1));
getch();
endwin();
return 0;
}
#include <ncurses.h>
#include <string.h>
int main()
{
initscr();
noecho();
cbreak();
int yMax, xMax;
getmaxyx(stdscr, yMax, xMax);
WINDOW *inputwin = newwin(3, xMax - 12, yMax - 5, 5);
box(inputwin, 0, 0);
refresh();
wrefresh(inputwin);
keypad(inputwin, true);
int c = wgetch(inputwin);
if (c == KEY_UP)
{
mvwprintw(inputwin, 1, 1, "YOU PRESSED KEY_UP!");
wrefresh(inputwin);
}
getch();
endwin();
return 0;
}
#include <ncurses.h>
#include <string.h>
int main()
{
initscr();
noecho();
cbreak();
int y, x, yBeg, xBeg, yMax, xMax;
// WINDOW *win = newwin(height, width, start_y, start_x);
WINDOW *win = newwin(10, 20, 10, 10);
getyx(stdscr, y, x);
getbegyx(stdscr, yBeg, xBeg);
getmaxyx(stdscr, yMax, xMax);
mvprintw(yMax / 2, xMax / 2, "%d %d", yBeg, xBeg);
// printw("%d %d %d %d %d %d", y, x, yBeg, xBeg, yMax, xMax);
getch();
endwin();
return 0;
}
#include <ncurses.h> int main() { // initialize the screen // sets up memory and clears the screen initscr(); cbreak(); raw(); noecho(); int height, width, start_x, start_y; height = 10; width = 20; start_x = start_y = 10; WINDOW *win = newwin(height, width, start_y, start_x); refresh(); char a = 's'; char b = 'b'; char c = '+'; // box(win, 0, 0); int left, right, top, bottom, tlc, trc, blc, brc; left = right = (int)a; top = bottom = (int)b; tlc = trc = blc = brc = (int)c; wborder(win, left, right, top, bottom, tlc, trc, blc, brc); mvwprintw(win, 1, 2, "this is my box"); wrefresh(win); // whats for user input,return int value of that key getch(); endwin(); // deallocates memory and ends ncurses return 0; }