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

devtools/2025/3/25 14:10:06/

简介

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


http://www.ppmy.cn/devtools/170588.html

相关文章

ubuntu桌面图标异常——主目录下的所有文件(如文档、下载等)全部显示在桌面

ubuntu桌面图标异常 问题现象问题根源系统级解决方案方法一:全局修改(推荐多用户环境)方法二:单用户修改(推荐个人环境)操作验证与调试避坑指南扩展知识参考文档问题现象 主目录文件异常显示 用户主目录(如/home/user/)下的所有文件(如文档、下载等)全部显示在桌面,…

css white-space: pre-line; 用处大

1.normal:忽略多余的空白&#xff0c;只保留一个空白&#xff08;默认&#xff09;&#xff1b; 2.pre:保留空白(行为方式类似于html中的pre标签)&#xff1b; 3.nowrap:只保留一个空白&#xff0c;文本不会换行&#xff0c;会在在同一行上继续&#xff0c;直到遇到br标签为止。…

DAPO:一个开源的大规模大型语言模型LLM强化学习系统

推断扩展赋予了大型语言模型前所未有的推理能力,强化学习作为激发复杂推理的核心技术,清华大学联合字节提出了解耦片段与动态采样策略优化(DAPO)算法,并全面开源了一个最先进的大规模强化学习系统,该系统使用Qwen2.5-32B基础模型在AIME 2024上取得了50分的高分。还开源了…

(滑动窗口)算法训练篇11--力扣3.无重复字符的最长字串(难度中等)

目录 1.题目链接&#xff1a;3.无重复字符的最长字符 2.题目描述&#xff1a; 3.解法(滑动窗口)&#xff1a; 1.题目链接&#xff1a;3.无重复字符的最长字符 2.题目描述&#xff1a; 给定一个字符串 s &#xff0c;请你找出其中不含有重复字符的 最长 子串 的长度。 示例…

掌握 Zapier:从入门到精通的自动化指南

1. 引言 在当今数据驱动的时代,自动化已成为企业提升效率和降低成本的核心策略。Zapier 作为全球领先的自动化平台,以其简单易用和强大的集成能力,帮助数百万用户连接不同的应用程序,实现业务流程的自动化。本文将全面介绍 Zapier 的使用方法,从基础概念到高级技巧,帮助…

强大的AI网站推荐(第二集)—— V0.dev

网站&#xff1a;V0.dev 号称&#xff1a;前端开发神器&#xff0c;专为开发人员和设计师设计&#xff0c;能够使用 AI 生成 React 代码 博主评价&#xff1a;生成的UI效果太强大了&#xff0c;适合需要快速创建UI原型的设计师和开发者 推荐指数&#xff1a;&#x1f31f;&…

CSS基础知识一览

持续维护 选择器 display 常用属性 浮动 弹性布局

笔记:代码随想录算法训练营day59:110.字符串接龙 、105.有向图的完全可达性、106.岛屿的周长

学习资料&#xff1a;代码随想录 110. 字符串接龙 卡码网题目链接&#xff08;ACM模式&#xff09; 还是有些许复杂&#xff0c;要把字符串从begin开始遍历&#xff0c;然后把每一个字母都换一下&#xff0c;看能否在字典里找到&#xff0c;如果能找到就入队列并记录&#x…