ÒÔǰѧ¹ýTC2.0µÄÅóÓѿ϶¨»¹¼ÇµÃTCÀï±ßÓÐÒ»¸öͼÐοâBGI(Borland Graphics Interface, »¹¼ÇµÃÄǸö
cursesµÄÃû×ÖÆðÔ´ÓÚ"cursor optimization",¼´¹â±êÓÅ»¯. Ëü×îÔçÓÉÓÐÃÀ¹ú²®¿ËÀû´óѧµÄBill JoyºÍKen Arnold±àдµÄ, ÓÃÀ´´¦ÀíÒ»¸öÓÎÏ·rogueµÄÆÁÄ»ÏÔʾ. ºóÀ´±´¶ûʵÑéÊÒµÄMark HortonÔÚSystem III UnixÖÐÖØбàдÁËcurses. ÏÖÔÚ¼¸ºõËùÓеÄUnix, Linux²Ù×÷ϵͳ¶¼´øÁËcursesº¯Êý¿â, cursesÒ²¼ÓÈëÁ˶ÔÊó±êµÄÖ§³Ö, һЩ²Ëµ¥ºÍÃæ°åµÄ´¦Àí. ¿ÉÒÔ˵, cursesÊÇLinuxÖÕ¶ËͼÐαà³ÌµÄ²»¶þÑ¡Ôñ(±ÈÈçÖøÃûµÄÎÄ×Ö±à¼Æ÷ vi ¾ÍÊÇ»ùÓÚcurses±àµÄ)
OK, Ïл°ÉÙ˵,ÏÖÔÚÎÒÃÇ¿ªÊ¼½øÈëÕýÌâ:
Ê×ÏÈÎÒÃÇÓ¦¸ÃÁ˽â, ÔÚÖÕ¶ËʹÓÃʹÓõÄÆÁĻģʽÊÇ»ùÓÚÎı¾µÄ. ËùÒÔÔÚ¿ªÊ¼Ê¹ÓÃcursesÇ°, ÐèÒªÓÃinitscr()º¯Êý³õʼ»¯ÆÁÄ». ¶ÔÓ¦µÄ, ³ÌÐò½áÊøÐèÒªµ÷ÓÃendwin();º¯ÊýÀ´¹Ø±Õcurses״̬.
ÓÐÁËÕâ¸ö¸ÅÄî, ÎÒÃǾͿÉÒÔÀ´Ð´ÖøÃûµÄ"Hello, world!"Ö®curses°æÁË:
ÒýÓÃ:/*-----------------------------------------------------------------
A very simple example of curses programming
coder: jellen
date: 3-26-2004
----------------------------------------------------------------*/
#include
int main()
{
initscr();
box(stdscr, ACS_VLINE, ACS_HLINE); /*draw a box*/
move(LINES/2, COLS/2); /*move the cursor to the center*/
waddstr(stdscr, "Hello, world!");
refresh();
getch();
endwin();
return 0;
}
ºÇºÇ, ¸Õ²ÅÍüÁË˵ÁË. ÎÒÃǵ÷ÓÃcurses¿âÒ»°ãÊÇÓÃCÓïÑÔµÄ(ûʲôÆæ¹Ö, C¿ÉÒÔ˵ÊÇLinuxµÄ¹Ù·½ÓïÑÔ, ²»¹ýÄãÒ²¿ÉÒÔÓÃC++»òPythonµÈÓïÑÔµ÷ÓÃcursesµÄ, ÕâÎ޹ؽôÒª). ¼ÙÉèÎÒÃÇ°ÑÕâ¸ö³ÌÐò±£´æΪ hello.c
²»¹ý²»Òª¼±×ÅÓÃgcc -o hello hello.cÈ¥±àÒëÔËÐÐ(ÎÒÖªµÀÄã»áÓÃgcc:) ÄÇÑùÊDz»ÄÜͨ¹ý±àÒëµÄ. ÒòΪcurses¿â²»ÔÚ±ê׼·¾¶ÉÏ, ËùÒÔÎÒÃÇÒª¼ÓÉÏ -lcursesÁ¬½ÓÑ¡Ïî, ÏñÕâÑù:
gcc -o hello hello.c -lcurses
ÏÖÔÚÄãÓà ./hello ÔËÐÐһϳÌÐò¿´¿´,ÊDz»ÊdzöÏÖÁËÄãËùÆÚÍûµÄ´°¿Ú.
ÏÖÔÚÎÒÃÇÀ´Ò»ÐÐÐзÖÎöһϴúÂë.
ÒýÓÃ:#include/*ÕâÊÇÿ¸ö curses ³ÌÐò¶¼±ØÐë°üº¬µÄÍ·Îļþ,±íÃ÷ʹÓÃÁËcurses¿â*/
È»ºóÖ÷º¯ÊýÖеÚÒ»¾äinitscr();³õʼ»¯ÁËÆÁÄ», ʹ֮¿ªÊ¼½øÈëcursesͼÐλ¯¹¤×÷·½Ê½.
ÆäʵÎÒÃÇÏÖÔÚûÓÐ×Ô¼º½¨Á¢´°¿Ú, ÓõÄÊDZê×¼ÆÁÄ» stdscr(¾ÍÏóCÀïÃæ±ê×¼ÊäÈëstdin, ±ê×¼Êä³östdoutÒ»¸ö¸ÅÄî), Ëü¾ÍÊÇÎÒÃÇÃæÇ°µÄµçÄÔÆÁÄ»(²»¹ýÏÖÔÚ»¹ÊǺںڵÄ)
ÏÂÃæÒ»¾ä: box(stdscr, ACS_VLINE, ACS_HLINE); »ÁËÒ»¸ö¿ò. ÓÐÁËÕâ¸ö¿òÎÒÃDzÅÓÐ"´°Ìå"µÄ¸Ð¾õ. stdscr¾ÍÊDZê×¼ÆÁÄ», ACS_VLINEºÍACS_HLINE´ú±í¹¹³É·½¿òÁ½±ßµÄ»ù±¾ÔªËØ, ÄãÒ²¿ÉÒÔÓà ''''|''''ºÍ''''-''''´úÌæ, ²»¹ý¿ÉÄÜûÓÐACS_VLINE, ACS_HLINEºÃ¿´ÁË.
ÒýÓÃ:move(LINES/2, COLS/2);
waddstr(stdscr, "Hello, world!");
ÕâÁ½¾äÊǰѹâ±êÒƵ½ÆÁÄ»Öмä, È»ºóÊä³öÎÒÃǵÄ"Hello, world!"
LINES ºÍ COLSÊÇcurses¶¨ÒåµÄºê, ´ú±íµ±Ç°ÆÁÄ»µÄ×î´óÐÐÊýºÍÁÐÊý. waddstr()º¯ÊýµÄ×÷ÓÃÊÇÔÚstdscrÉÏ´òÓ¡×Ö·û´®"hello, world!"
ÆÁÄ»·ÖÎïÀíÆÁÄ»(ÎÒÃÇËù¿´µ½µÄ)ºÍÂß¼ÆÁÄ»(ÔÚÄÚ´æÖеÄ), ÎÒÃǵ÷Óú¯ÊýʱÐ޸ĵÄÊÇÂß¼ÆÁÄ»,Ëü²»»áÔÚµ±Ç°ÎïÀíÆÁÄ»ÉÏÏÔʾ³öÀ´.ËùÒÔÏÖÔÚÆÁÄ»ÉÏ»¹ÊÇʲôҲûÓÐ, ÐèÒªµ÷ÓÃrefresh()°ÑÎÒÃǶÔÂß¼ÆÁÄ»µÄ¸Ä¶¯ÔÚÎïÀíÆÁÄ»(ÏÔʾÆ÷)ÉÏÏÔʾ³öÀ´. È»ºóÓÃgetch()ÈÃÆÁÄ»ÔÝÍ£Ò»ÏÂ.
×îºóµ÷ÓÃendwin()½áÊøcurses, »Ö¸´ÔÀ´µÄÆÁÄ».
ºÃÁË, ÎÒÃÇÍê³ÉÁ˵ÚÒ»¸öÀý×ӵķÖÎö. ÊDz»ÊÇͦ¼òµ¥µÄ?
²»¹ý²»¾¡ÈçÈËÒâµÄÊÇÆÁÄ»»¹ÊÇÄǸöÑù×Ó, ûÓÐʲôɫ²Ê. Òª¼ÓÉ«²Ê? ÄÇÒ²ÊÇͦÈÝÒ×µÄ: Ê×ÏÈÓÃstart_color()º¯Êý¿ªÆôÑÕɫģʽ, È»ºóÉèÖÃÎÒÃÇÒªµÄÑÕÉ«¾ÍÐÐÁË.
cursesÀïµÄÑÕÉ«ÊÇÅä¶ÔµÄ, ÒªÒ»¸ö±³¾°É«¶ÔÒ»¸öÇ°¾°É«. ʹÓÃ֮ǰÓÃinit_pair()³õʼ»¯.
±ÈÈçinit_pair(1, COLOR_BLUE, COLOR_GREEN);¾Í¶¨ÒåÁËÒ»×éÑÕÉ«, COLOR_BLUEΪǰ¾°É«, COLOR_GREENΪ±³¾°É«. 1ÊÇËüÃǵıê¼ÇºÅÂë(¹©ÆäËûº¯Êýµ÷ÓÃʱʹÓÃ) ÎÒÃÇÀ´¿´Ò»¸ö¼òµ¥µÄÀý×Ó:
ÒýÓÃ:/*--------------------------------------------------------------
A simple curses color demo program
Coder: jellen
Date: 3-26-2004
-------------------------------------------------------------*/
#include
int main()
{
initscr(); /*³õʼ»¯ÆÁÄ»*/
if(start_color() == OK) /*¿ªÆôÑÕÉ«*/
{
init_pair(1, COLOR_RED, COLOR_GREEN); /*½¨Á¢Ò»¸öÑÕÉ«¶Ô*/
attron(COLOR_PAIR(1)); /*¿ªÆô×Ö·ûÊä³öÑÕÉ«*/
move(LINES/2, COLS/2);
waddstr(stdscr, "Yet another Hello, world!");
attroff(COLOR_PAIR(1)); /*¹Ø±ÕÑÕÉ«ÏÔʾ*/
refresh();
}
else
{
waddstr(stdscr, "Can not init color");
refresh();
}
endwin(); /*¹Ø±Õcurses״̬*/
return 0;
}
Õâ¸ö³ÌÐò¼ÙÈç±£´æΪcolor.c
ÄÇôÄã¿ÉÒÔÕâÑù±àÒëÁË: gcc -o color color.c -lcurses
ÓÃ./colorÒ»ÔËÐиö²ÊÉ«µÄ×Ö? ºÇºÇ, ËäÈ»²»ºÃ¿´, µ«ÊÇ˵²»¶¨Äã×Ô¼ºÒÔºó¿ÉÒÔÓÃcursesд³ö¾«ÃÀµÄ³ÌÐò.