分类: LINUX
2008-12-13 15:35:58
Initscr和endwin.函数:
#include
WINDOW
*initscr(void);
int
endwin(void);
函数说明:
The initscr
function must only be
called once in each program. The initscr function returns a
pointer
to the stdscr structure if it succeeds. If it fails, it simply prints a diagnostic error
message and
causes
the program to exit
The endwin
function returns OK on success and ERR
on failure. You can
call endwin to leave curses
and
then later resume curses operation by calling clearok(stdscr,
1) and refresh. This effectively
makes curses
forget what the
physical screen looks like and forces it to perform a complete redisplay
*
屏幕输出
屏幕刷新函数:
#include
int addch(const chtype char_to_add);
int addchstr(chtype *const string_to_add);
int printw(char *format, ...);
int refresh(void);
int box(WINDOW *win_ptr, chtype vertical_char, chtype
horizontal_char);
int insch(chtype char_to_insert);
int insertln(void);
int delch(void);
int deleteln(void);
int beep(void);
int flash(void);
curses自己的字符类型,比char要大,在标准linux中,chtype实际是typedef for unsigned long。
函数说明:
The add...
functions add the
character or string specified at the current location. The printw
function
formats
a string in the same way as printf and adds it to the current location. The refresh
function
causes
the physical screen to be updated, returning OK on success and ERR
if an error occurred.
The box
function
allows you to draw a box around a window.
标准curses中,一般只使用普通字符。在扩展中,可以使用ACS_VLINE and ACS_HLINE。
The insch
function inserts a character, moving existing characters right, though what
will happen at the end
of a
line isn’t specified and depends on the terminal you’re using. insertln inserts
a blank line, moving
existing
lines down by one. The two delete functions are analogous to the two insert functions.
To make a sound, you can call beep. A very small number
of terminals are unable to make any sound, so
some curses
setups will cause the screen to flash when beep is called. If you work in a
busy office, where
beeps
can come from any number of machines, you might find you prefer this. As you
might expect, flash
causes
the screen to flash, but if this isn’t possible, it tries to make a sound on
the terminal instead.
*
屏幕读取
一般不怎么使用,因为屏幕上的内容自己一般知道。
#include
chtype inch(void);
int instr(char *string);
int innstr(char *string, int number_of_characters);
函数说明:
The inch
function should
always be available, but the instr and innstr functions are not always
supported.
The inch function returns a character and its attribute information from the
current screen
location
of the cursor. Notice that inch doesn’t return a character, but a chtype, while instr
and
innstr write to arrays of chars.
*
清屏
四种方法:
#include
int erase(void);
int clear(void);
int clrtobot(void);
int clrtoeol(void);
函数说明:
The erase
function writes
blanks to every screen location. The clear function, like erase, clears the
screen,
but forces a screen redisplay by internally calling a lower-level function, clearok, which enforces
a
clear screen sequence and redisplay when the next refresh
is called.
The clear
function usually uses
a terminal command that erases the entire screen, rather than simply
attempting
to erase any currently nonblank screen locations. This makes the clear
function a reliable
way of
completely erasing the screen. The combination of clear
followed by refresh
can provide a
useful
redraw command if the screen display has become confused or corrupted in some
way.
clrtobot clears the screen from the cursor position onward to the
end
*
光标移动
移动光标,光标走后屏幕的更新
#include
int move(int new_y, int new_x);
int leaveok(WINDOW *window_ptr, bool leave_flag);
函数说明:
The move
function simply moves
the logical cursor position to the specified location. Remember that the
screen
coordinates are specified with (0,0) as the top left-hand corner of the screen.
In most versions of
curses, the two extern integers LINES
and COLUMNS
contain the physical
screen size and can be used to
determine
the maximum allowed values for new_y and new_x. Calling move
won’t, in itself,
cause the physical
cursor
to move. It only changes the location on the logical screen at which the next
output will appear. If
you
want the screen cursor to move immediately after calling move, follow it with a call to refresh.
The leaveok
function sets a flag
that controls where curses leaves the physical cursor after a screen
update.
By default, the flag is false, and after a refresh the hardware cursor
is left in the same position
on the
screen as the logical cursor. If the flag is set to true, the hardware cursor may be left
randomly,
anywhere
on the screen. Generally, the default option is preferred to ensure that the
cursor is left in a
sensible
location.
*
字符属性
定义的属性有:A_BLINK, A_BOLD,
A_DIM, A_REVERSE, A_STANDOUT,
and A_UNDERLINE.
#include
int attron(chtype attribute);
int attroff(chtype attribute);
int attrset(chtype attribute);
int standout(void);
int standend(void);
函数说明:
The attrset
function sets the curses
attributes, attron
and attroff
turn on and off
specified attributes
without
disturbing others, while standout and standend provide a more generic emphasized, or
“stand
out” mode. This is commonly mapped to reverse video on most terminals.
实例:移动,插入和属性。
# cat moveadd.c
/* We include the following header files,
define some character arrays and a pointer
to those arrays
and then initialize the curses
structures. */
#include
#include
#include
#include
#include
int main()
{
const char witch_one[] = " First
Witch ";
const char witch_two[] = " Second
Witch ";
const char *scan_ptr;
initscr();
/* Now for the three initial sets of text that appear
at intervals on the screen.
Note the on and off flagging of text
attributes. */
move(5, 15);
attron(A_BOLD);
printw("%s",
"Macbeth");
attroff(A_BOLD);
refresh();
sleep(1);
move(8, 15);
attron(A_STANDOUT);
printw("%s", "Thunder and
Lightning");
attroff(A_STANDOUT);
refresh();
sleep(1);
move(10, 10);
printw("%s", "When shall we
three meet again");
move(11, 23);
printw("%s", "In thunder,
lightning, or in rain ?");
move(13, 10);
printw("%s", "When the hurlyburly's done,");
move(14,23);
printw("%s", "When the
battle's lost and won.");
refresh();
sleep(1);
/* Lastly, the actors are identified and their
names are inserted a character
at the time.
We also add the reset
function at the end of the main function.
*/
attron(A_DIM);
scan_ptr = witch_one + strlen(witch_one) -
1;
while(scan_ptr != witch_one) {
move(10,10);
insch(*scan_ptr--);
}
scan_ptr = witch_two + strlen(witch_two) -
1;
while (scan_ptr != witch_two) {
move(13, 10);
insch(*scan_ptr--);
}
attroff(A_DIM);
refresh();
sleep(1);
move(LINES - 1, COLS - 1);
refresh();
sleep(1);
endwin();
exit(EXIT_SUCCESS);
}