Linux programming learning notes (11) curses Cui Interface

Source: Internet
Author: User
Tags set background

Cui character Interface
Gui: graphical interface

Use a set of encapsulated library libcruses. So
Old Version libcurses. So
New Version libncruses. So
-Lcurses or-lncurses are required during compilation.
If the header file curses. h does not exist, use ncurses. h.

Printf/scanf standard Io
Most standard Io redirection to/dev/tty/det/pts/1

Curses is the terminal output.

To prevent the UI from being redirected to the terminal by printf, do not use printf output.

1 Programming Model

Initialize the terminal window * initscr ();
Returns an initialized form.
Operation terminal (input/output/location/refresh)
Release terminal int endwin ()
2 display

2.1 Graphic Output

Border prints a border
Int border (chtype ls, chtype RS, chtype ts, chtype BS,
Chtype TL, chtype TR, chtype BL, chtype BR );
The eight parameters are the characters in the upper left, lower left, upper right, lower left, and lower right.
You can use 8 zeros to use the default border.
Border ('A', 'B', 'C', 'E', 'F', 'G', 'h', 'G ');
Border (0, 0, 0, 0, 0, 0 );
Wborder draws a border in the specified form

Box prints a border
Int box (window * Win, chtype verch, chtype Horch );
Box requires a form. Only horizontal and vertical border characters can be set.
In addition to returning init_scr (), the standard screen can also use the global variable stdscr.
Box (stdscr,); the effect is the same as border (,);

Hline horizontal line
Hline draws a horizontal line at the cursor position of the standard form
Whline draws a horizontal line at the cursor position of the specified form
Mvhline draws a horizontal line at a specified position in the standard form
Mvwhline draws a horizontal line at the specified form pointer position

Vline vertical line (same as hline)

Attribute character: byte = attribute byte + character byte
Note:
Box requires a form
Initscr returns the initialized form: standard screen
Curses defines a global variable stdscr to represent the standard form.
Naming rules:
*** A function
W *** run a function in a form
MV *** runs a function at a specified position
Mvw ***
Run a function at the specified position in the specified form

2.2 screen Flushing

Void refresh (); // display the output immediately. Generally, refresh is returned immediately after the output.
Void wrefresh (window *);
Screen flushing from the inside out



3-character output

Int addch (const chtype ch );
Int waddch (window * Win, const chtype ch );
Int mvaddch (INT y, int X, const chtype ch );
Int mvwaddch (window * Win, int y, int X, const chtype ch );
Attribute character: ''| attribute
(Character bit budget character attribute)


Attribute (in man attron)
A_normal normal display (no highlight)
A_standout best highlighting mode of the terminal.
A_underline underlining
A_reverse Reverse Video
A_blink blinking
A_dim half bright
A_bold extra bright or bold
A_protect Protected Mode
A_invis invisible or blank Mode
A_altcharset alternate Character Set
A_chartext bit-mask to extract a character
Color_pair (n) color-Pair number n

Special characters
Man addch can also find characters that do not know how to print, such as pi
Acs_pi * Greek pi
Acs_plminus # plus/minus
Acs_plus + plus
Acs_rarrow> arrow pointing right
Acs_rtee + right tee
Acs_s1-Scan Line 1
And so on.


3-character attribute and color

Color Attribute

3.1 determine whether the terminal supports color (currently, terminals generally support color, but can not be determined)

Bool has_colors (void );

3.2 initialize color

Int start_color (void );

3.3 define color pairs

Int init_pair (short pair, short F, short B );
No. foreground color background color
Up to 8 pairs of colors 1-8 are supported
Optional colors:
Color_black
Color_red
Color_green
Color_yellow
Color_blue
Color_magenta purple
Color_cyan cyan
Color_white

If (has_colors () = true)
{
Start_color (); // initialize the color
Init_pair (1, color_red, color_yellow );
Init_pair (2, color_blue, color_white );
Init_pair (3, color_black, color_white );
Bkgd (color_pair (3 ));
}

3.4 use a color pair (macro)

Color_pair (short pair)
Set Character Color in character attributes
Mvaddch (2,10, 'A' | a_underline | color_pair (1 ));
Set background color bkgd Function
Init_pair (3, color_black, color_white );
Bkgd (color_pair (3); // sets the background color.


This set of functions must be used after init_scr

# Include <curses. h>
Int main ()
{
Initscr (); // initialize the form
If (has_colors () = true)
{
Start_color (); // initialize the color
Init_pair (1, color_red, color_yellow); // defines the color pair
Init_pair (2, color_blue, color_cyan );
Init_pair (3, color_black, color_white );
Bkgd (color_pair (3); // sets the background color.
}
Box (stdscr, 0, 0); // output border
Mvaddch (2, 10, 'A' | a_underline | color_pair (1); // output character
Mvaddch (10, 20, 'B' | a_bold | color_pair (1 ));
Mvaddch (10, 21, acs_pi | color_pair (2); // output special characters

Getch ();
Endwin (); // release the form
}

3.5 output string addstr

Int addstr (const char * Str); Output string
Int addnstr (const char * STR, int N); the first n characters of the output string
Int waddstr (window * Win, const char * Str );
Int waddnstr (window * Win, const char * STR, int N );
Int mvaddstr (INT y, int X, const char * Str );
Int mvaddnstr (INT y, int X, const char * STR, int N );
Int mvwaddstr (window * Win, int y, int X, const char * Str );
Int mvwaddnstr (window * Win, int y, int X, const char * STR, int N );

Mvaddstr (, "Hello World"); // output string

3.6 Format String output printw

Int printw (const char * FMT ,...);
Int wprintw (window * Win, const char * FMT ,...);
Int mvprintw (INT y, int X, const char * FMT ,...);
Int mvwprintw (window * Win, int y, int X, const char * FMT ,...);

3.7 set attributes

Attron () Enable attributes
Attroff () Disable attributes

Attron (color_pair (1); // enable attributes
Mvaddstr (, "Hello World"); // output string
Attroff (color_pair (1); // disable attributes

Attron (color_pair (2) | a_underline); // enable two attributes
Mvaddnstr (3,10, "hahahahaha", 3); // control the first few characters of the output string. 3
Attroff (color_pair (2) | a_underline); // close two attributes

Case 1:

/* Write a time display screen initialization cycle display time and refresh and release resources */# include <curses. h> # include <time. h> // localtime # include <unistd. h> // sleepvoid Init (); void drawui (); void bussiness (); void destory (); int main () {Init (); drawui (); bussiness (); destory ();} void Init () {initscr ();} void destory () {endwin ();} void drawui () {box (stdscr, 0, 0);} void bussiness () {time_t tt; struct TM * t; while (1) {TT = time (0); t = localtime (& TT ); mvprintw (lines/2, (Cols-8)/2, "% 02d: % 02d: % 02d", T-> tm_hour, T-> tm_min, t-> tm_sec); refresh (); // be sure to refresh or not show sleep (1 );}}

Case 2:
/*
Logon page
1 Initialization
2. Drawing Interface
Header
Draw user name input area
Drawing Password Input Area
3. Waiting for Input
4. End
*/

# Include <unistd. h> # include <curses. h> # include <string. h> // # include <stdlib. h> void Init (); void drawlogin (); void destory (); int main () {Init (); drawlogin (); destory (); Return 0 ;} void Init () {initscr ();} void drawlogin () {char * Heads = "BSS bussiness support system"; char * user = "User []"; char * Pwd = "PWD []"; box (stdscr, 0, 0); attron (a_bold); mvaddstr (3, (Cols-strlen (heads)/2, heads ); attroff (a_bold); mvhline (4, (Cols-strlen (heads)/2, 0, strlen (heads); mvaddstr (8, (Cols-strlen (User )) /2, user); mvaddstr (10, (Cols-strlen (PWD)/2, PWD);} void destory () {refresh (); getch (); // press one key to launch endwin ();}
4 input

4.1 Character Input

Int getch (void); returns the input character and returns the echo at the cursor. The cursor moves forward.
Int wgetch (window * Win );
Int mvgetch (INT y, int X); returns the input character and displays it in the specified place.

Int mvwgetch (window * Win, int y, int X );

#include <unistd.h>#include <curses.h>#include <string.h>//#include <stdlib.h>void init();void draw();void destory();int main(){init();draw();destory();return 0;}void init(){initscr();}void draw(){char ch;while(1){ch=mvgetch(LINES/2,COLS/2);mvprintw(20,10,"your input is %c(%d)",ch,ch);refresh();}}void destory(){ endwin();}

Some buttons display incorrect results in man getch.
Key_break Break Key
Key_down the four arrow keys...
Key_up
Key_left
Key_right
Key_home Home Key (upward + left arrow)
Key_backspace backspace
Key_f0 function keys; space for 64 keys
Is reserved.

4.2 control functions

Disable noecho (with ECHO by default)
Int noecho (void );

Make the function key valid (the function key is disabled by default)
Int keypad (window * Win, bool BF );


// Output a character and move the character with the direction key

# Include <curses. h> int main () {int CH, x = 5, y = 5; initscr (); noecho (); // prevent echo keypad (stdscr, true ); // make the function key valid mvaddch (Y, X,'s); While (1) {CH = getch (); mvaddch (Y, X ,''); switch (CH) {Case key_up: y --; break; Case key_down: Y ++; break; Case key_left: X --; break; Case key_right: X ++; break ;} if (x <0 | Y <0) {clear ();} mvaddch (Y, X, 'A'); refresh ();} endwin (); return 0 ;}

4.3 clear screen

Int erase (void); // Delete the entire Screen
Int werase (window * Win );
Int clear (void); // Delete the entire Screen
Int wclear (window * Win );
Int clrtobot (void); // Delete to the end of the screen
Int wclrtobot (window * Win );
Int clrtoeol (void); // delete it to the end of the row
Int wclrtoeol (window * Win)

4.4 cursor control

1. Get the cursor position (this is a macro)
Void getsyx (INT y, int X );
Set parameter location
Void setsyx (INT y, int X );
Set whether the cursor is visible
Int curs_set (INT visibility); 0 invisible

4.5 input string

Int getstr (char * Str );
Int getnstr (char * STR, int N );
Int wgetstr (window * Win, char * Str );
Int wgetnstr (window * Win, char * STR, int N );
Int mvgetstr (INT y, int X, char * Str );
Int mvwgetstr (window * Win, int y, int X, char * Str );
Int mvgetnstr (INT y, int X, char * STR, int N );

4.6 format the input string int scanw (char * FMT ,...);
Int wscanw (window * Win, char * FMT ,...);
Int mvscanw (INT y, int X, char * FMT ,...);
Int mvwscanw (window * Win, int y, int X, char * FMT ,...);

#include <unistd.h>#include <curses.h>#include <string.h>//#include <stdlib.h>void init();void draw();void dealInput();void destory();int main(){init();draw();dealInput();destory();return 0;}void init(){initscr();}void draw(){mvaddstr(2,2,"[     ]+[     ]=[      ]");}void dealInput(){int a,b,ch;while(1){mvaddstr(2,3,"     ");mvaddstr(2,11,"     ");mvaddstr(2,19,"     ");mvscanw(2,3,"%d",&a);mvscanw(2,11,"%d",&b);mvprintw(2,19,"%d",a+b);refresh();mvaddstr(3,3,"is continue? (y/n)");ch = getch();if(ch == 'n'){break;}}}void destory(){ endwin();}

5 windows

For good looks
Subwin (); // create a subform (coordinates of the subform are used)
Derwin (); // create a child form (coordinates of the parent form are used)

Window * Derwin (window * orig, int nlines, int ncols,
Int begin_y, int begin_x );

#include <curses.h>int main(){initscr();WINDOW *w;box(stdscr,0,0);w=derwin(stdscr,10,20,2,2);box(w,0,0);refresh();getch();endwin();}

1. Set encoding in VI
: Set enconding = encoding gd2312 UTF-8 ios-8859-1
2. Specify the source file encoding in the compiler-finput-charset = gb2312
3. Specify the encoding on the terminal
4. Default System Encoding

Related Article

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.