linux-curses function Library

Source: Internet
Author: User
Tags clear screen

Curses Library Basic Concepts

Control the format of character input/output
Termios drawbacks, escaping processing
Curses advantages
Provides terminal-independent character handling
Can manage keyboard
Support for multi-form management

curses vs. ncurses源文件包含头文件curses.h编译时加 –lcurses选项gcc program.c ?o program ?lcursesgcc ?I/usr/include/ncurses program.c ?o program ?lncursescurses配置情况的检查查看头文件:ls ?l /usr/include/*curses.h查看库文件:ls ?l /usr/lib/*curses.*cureses工作于屏幕、窗口和子窗口上屏幕:正在写的设备,占据设备上的全部可用显示面积窗口curses窗口(称为stdscr):至少存在一个,与物理屏幕的尺寸相同其他窗口:尺寸小于屏幕窗口,可以重叠子窗口:必须包含在父窗口内
Core data structure of curses library
stdscr对应于“标准屏幕”,是curses程序的默认输出窗口工作原理与stdio函数库中的stdout非常相似在curses函数产生输出时被刷新在调用refresh函数之前,输出到stdscr上的内容不会在屏幕上显示curscr对应当前屏幕外观调用refresh函数时,curses函数库比较stdscr及curscr之间的不同之处,然后用两个数据结构之间的差异来刷新屏幕curses程序需了解stdscr,但不需要使用curscr数据结构
The process of outputting characters in a curses program

Refresh the logical screen with the curses function
Refresh the physical screen with the refresh function
Logic screen
Using a character array to implement
Top left corner of the screen-coordinates (0, 0) as the starting point
Coordinate form
Y in front, indicates line number
X After, indicates the column number
The characters and their properties for each location including the screen position

Global variables in the curses
WINDDW* curscr:当前屏幕  WINDOW* stdscr:标准屏幕  int LINES:终端上的行数  int COLS:终端上的列数  boolTRUE:真标志,1  boolFALSE:假标志,0  int ERR:错误标志,-1  int OK:OK标志,0  
Curses screen processing
函数定义#include <curses.h>WINDOW *initscr(void);在一个程序中只能调用一次判断终端类型和初始化Curses数据结构,同时也对终端进行一次刷新以清除屏幕,为以后的操作做准备 成功时,返回一个指向stdscr结构的指针失败时,返回一个诊断信息并使程序结束
函数定义int endwin(void1)调用refresh函数

Refresh function

函数定义intrefresh(void);说明curses最常用的一个函数在调用屏幕输出函数试图改变屏幕上的画面时,curses并不会立刻对屏幕做改变,而是等到refresh()调用后,才将刚才所做的变动一次完成其余信息维持不变,以尽可能送最少字符发送至屏幕上,减少屏幕重绘时间如果是initscr()后第一次调用refresh(),curses将做清除屏幕的工作
Basic screen character output processing function
int  addch ( const  chtype ch); //enter a single character at the current cursor position, and move the cursor one bit to the right, with the addition of the character modifier parameter class function  int  addchstr (chtype *const  string_to_add); Span class= "Hljs-comment" >//enter a string at the current cursor position  int  printw ( char  *format, ...); //is formatted with the same mechanism string as printf and adds it to the cursor's current position  int  Insch (chtype ch); //the character ch is inserted to the left of the cursor, all characters following the cursor move to the right one position at the far right of this line may be lost  int  insertln (void ); //insert a blank line, moving the existing row down one line at a time   
int delch(void);//删除光标左边字符,并把光标右边余下的字符向左移动一个位置 int deleteln(void);//删除光标下面的一行,并把下面所有的其他行都向上移动一个位置, 此外,屏幕最底下的一行将被清除int box(WINDOW *win_ptr, chtype vertical_char, chtype horizontal_char);//自动画方框vertical_char :画方框时垂直方向所用字符horizontal_char:画方框时水平方向所用字符int beep(void);//让程序发出声音int flash(void);//使屏幕闪烁
Reading basic functions from the screen
chtype inch(void);返回光标当前位置的字符及其属性int instr(char *string);将返回内容写到字符数组中int innstr(char *stringint number_of_characters);将返回内容写到字符数组中,可以指定返回字符的个数
Clear Screen
int erase(void);在每个屏幕位置写上空白字符int clear(void);与erase()类似,也是清屏,但通过调用clearok函数来强制重现屏幕原文clearok函数强制执行清屏操作,并在下次调用refresh函数时重现屏幕原文int clrtobot(void);清除当前光标所在行下面的所有行,包括当前光标所在行中的光标位置右侧直到行尾的内容int clrtoeol(void);清除当前光标所在行中光标位置右边至行尾的内容
Move cursor
intmove(intint x);将光标移动至(y, x)的位置 LINES和COLUMNS决定y与x的最大取值本函数不会使物理光标移动,仅改变逻辑屏幕上的光标位置,下次的输出内容将出现在该位置上如果希望物理屏幕上的光标位置在调用move后立即变化,需要立即调用refresh函数intbool leave_flag);控制在屏幕刷新后curses将物理光标放置的位置默认情况下,该标志为false,意味屏幕刷新后,硬件光标将停留在屏幕上逻辑光标所处的位置如果该标志设为true,则硬件光标会被随机地放在屏幕上的任意位置
Curses character processing
控制字符在屏幕上的显示方式前提是用于显示的硬件设备能够支持要求的属性属性定义A_UNDERLINEA_REVERSEA_BLINKA_BOLDA_NORMAL:标准模式(只能配合attrset()使用)A_DIM:半亮显示A_STANDOUT
Character attribute control function
intattributeintattributeintattributeint standout(void);更加通用的强调或者突出显示模式在大多数终端上,通常映射成反白显示int standend(void

Read each character in the file and look for "/*"
Once found, call the Attron () function to start adding bold to the output text
When "* *" is found (mark at the end of the note)
Use the Attroff () function to turn off cosmetic effects

Curses keyboard handling
int echo(void);int noecho(voidint cbreak(void);int nocbreak(voidint raw(void);int noraw(void);打开或关闭RAW模式RAW模式不处理特别字符,此时不可以输入特殊字符序列来产生信号或进行流控制noraw同时恢复行模式和特殊字符处理功能  
Keyboard input
int getch(void);从键盘读取一个字符返回值是整数值int getstr(char *string);从键盘读取一串字符int getnstr(char *stringint number_of_characters);允许对读取的字符数目加以限制int scanw(char *format, ...);与scanf类似,从键盘读取一串字符
Curses form processing
 display several different sizes of Windows on the screen window structure STDSCR is a special case window  (int  lines, int  cols, int  y , int  x ), via screen position (y , x ) And specifying the number of rows lines and the number of columns cols to create a new window when successful, returns a pointer to a new window, creation failure returns NULL if you want the lower-right corner of the new window to be in the lower-right corner of the screen, you can specify the number of rows or columns as 0  all Windows must fit on the current screen, if any part of the new window is outside the screen area, Newwin function will fail int  delwin (Window *window_to_delete ), delete a window generated by Newwin do not attempt to delete curses own window, STDSCR and CURSCR  
............);
说明当添加w前缀时,一个额外的WINDOW指针必须添加到参数列表中当添加mv前缀时,两个额外的参数,y与x位置,必须添加到参数列表中。他们指明了执行操作的位置y与x是相对于窗口的,而不是屏幕,(0,0)是窗口的左上角
Move and update windows
intMvwin (WINDOW*window,int y,int x); Move a window on the screen if you move any part of the window out of the screen area, the Mvwin function failsintWrefresh (WINDOW*window);intWclear (WINDOW*window);intWerase (WINDOW*window); The above three functions point to a specific window instead of STDSCRintScrollok (WINDOW*window, bool Scroll_flag); When a Boolean value of true (usually non-0) is passed, a window is allowed to scroll by default, and the window cannot be scrolledintScroll (WINDOW*window); Scroll the window up one lineintTouchwin (WINDOW*window); Notifies the curses library that its parameters point to the window content has changed this means that the curses library will redraw the window the next time the Wrefresh is called, although there is no actual change in the window content when there are multiple windows stacked on the screen and need to decide which window to display, This function can be very useful

linux-curses function Library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.