欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 游戏 > 使用 ncurses 库创建文本用户界面:基础函数详解

使用 ncurses 库创建文本用户界面:基础函数详解

2025/3/29 17:29:26 来源:https://blog.csdn.net/yangyangyiyang_/article/details/146446049  浏览:    关键词:使用 ncurses 库创建文本用户界面:基础函数详解

简介

ncurses 是一个功能强大的库,用于在 Unix-like 系统中创建文本用户界面。它提供了丰富的函数来控制屏幕上的文本显示、处理键盘输入、绘制图形元素等。本文将详细介绍 ncurses 库中的一些基础函数,包括 printwwrefresh、获取用户信息、键盘输入、绘制图形元素以及获取终端信息等。

初始化和清理

在使用 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);  // 刷新窗口,显示内容

获取终端信息

getyxgetbegyxgetmaxyx 函数分别用于获取当前光标位置、窗口的起始坐标和最大坐标。

int y, x, yBeg, xBeg, yMax, xMax;
getyx(stdscr, y, x);  // 获取光标位置
getbegyx(stdscr, yBeg, xBeg);  // 获取窗口起始坐标
getmaxyx(stdscr, yMax, xMax);  // 获取窗口最大坐标

文本颜色和属性

ncurses 支持文本颜色和属性。attronattroff 函数用于设置文本属性,如加粗、反色等。

attron(COLOR_PAIR(1) | A_BOLD, NULL, NULL);  // 设置文本颜色和加粗
printw("This is bold and colored");  // 打印文本
attroff(COLOR_PAIR(1) | A_BOLD, NULL, NULL);  // 关闭文本属性

wattronncurses 库中的一个函数,用于打开(启用)窗口(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 screenWINDOW *menuwin = newwin(6, xMax - 12, yMax - 8, 5); // create a new window for the menuwinbox(menuwin, 0, 0);                                  // draw a box around the menurefresh();                                           // refresh the screen to show the changeswrefresh(menuwin);                                   // refresh the menu window// makes it so we can use arrow keyskeypad(menuwin, true);char choices[][20] = {"walk", "jog", "run"}; // array of strings for the menu choicesint choice;                                  // variable to store the user/s choiceint highlight = 0;                           // variable to track the highlight menu choicewhile (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 highlightedwattron(menuwin, A_REVERSE);          // turn on reverse video attributemvwprintw(menuwin, i + 1, 1, choices[i]); // print the menu choicewattroff(menuwin, A_REVERSE);             // get the user's choice from the menu window}choice = wgetch(menuwin); // get the user's choice from the menu windowswitch (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 keybreak;        // exit the loop}printw("Your choice was :%s", choices[highlight]); // print the user's choicegetch();  // wait for the user to press any key before exitingendwin(); // end the ncurses mode and restore the therminal settingsreturn 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 screeninitscr();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 keygetch();endwin();// deallocates memory and ends ncursesreturn 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词