C指針原理教程之Ncurses介紹
1、安裝Ncurses
Ncurses是一個能提供功能鍵定義(快捷鍵),屏幕繪制以及基于文本終端的圖形互動功能的動態(tài)庫。
Ncurses是一個能提供基于文本終端窗口功能的動態(tài)庫. Ncurses可以:
· 只要您喜歡,您可以使用整個屏幕
· 創(chuàng)建和管理一個窗口
· 使用8種不同的彩色
· 為您的程序提供鼠標(biāo)支持
· 使用鍵盤上的功能鍵
Ubuntu下
mysea@mysea-desktop:~$ sudo apt-get install libncurses5-dbg libncurses5-dev mysea@mysea-desktop:~/test$ gcc -lncurses -o cursestest cursestest.c
Freebsd下
cd /usr/ports/devel/ncurses-devel make install clean
2、hello,world
#include <curses.h>
int main(void){
initscr();//初始化
box(stdscr,ACS_VLINE,ACS_HLINE);//畫邊框
mvaddstr(15,2,"hello,world");//在15,2顯示字符串
refresh();//刷新屏幕
getch();//等待按鍵
endwin();//結(jié)束
return 0;
}
編譯及運(yùn)行
dp@dp:~/cursestest % gcc -lncurses 1.c -o mytest dp@dp:~/cursestest % ./mytest
3、色彩
然后編寫下面代碼:
#include <ncurses.h>
#include <locale.h>
#include <stdio.h>
int main(void){
//init_pair(short index,short foreground,short background)初始化顏色索引
//attron(COLOR_PAIR(索引號)|屬性)
setlocale(LC_ALL,"");
initscr();//初始化
box(stdscr,ACS_VLINE,ACS_HLINE);//畫邊框
if (!has_colors()||start_color()==ERR){
endwin();
printf("終端不支持顏色\n");
return 0;
}
init_pair(1,COLOR_GREEN,COLOR_BLACK);
init_pair(2,COLOR_RED,COLOR_BLACK);
init_pair(3,COLOR_WHITE,COLOR_BLUE);
int i=0;
for (i=1;i<=3;i++){
attron(COLOR_PAIR(i));
move(i,10);
printw("hello,world:%d",i);
}
for (i=1;i<=3;i++){
attron(COLOR_PAIR(i)|A_UNDERLINE);
move(i+5,10);
printw("hello,world:%d",i);
}
refresh();//刷新屏幕
getch();//等待按鍵
endwin();//結(jié)束
執(zhí)行
4、對中文的支持
dp@dp:~/cursestest % cat 1.c
#include <ncurses.h>
#include <locale.h>
#include <stdio.h>
int main(void){
//init_pair(short index,short foreground,short background)初始化顏色索引
//attron(COLOR_PAIR(索引號)|屬性)
setlocale(LC_ALL,"");
initscr();//初始化
box(stdscr,ACS_VLINE,ACS_HLINE);//畫邊框
if (!has_colors()||start_color()==ERR){
endwin();
printf("終端不支持顏色\n");
return 0;
}
init_pair(1,COLOR_GREEN,COLOR_BLACK);
init_pair(2,COLOR_RED,COLOR_BLACK);
init_pair(3,COLOR_WHITE,COLOR_BLUE);
int i=0;
for (i=1;i<=3;i++){
attron(COLOR_PAIR(i));
move(i,10);
printw("hello,世界%d",i);
}
for (i=1;i<=3;i++){
attron(COLOR_PAIR(i)|A_UNDERLINE);
move(i+5,10);
printw("hello,世界:%d",i);
}
refresh();//刷新屏幕
getch();//等待按鍵
endwin();//結(jié)束
return 0;
}
編譯時注意要使用ncursesw庫,不使用ncurses庫
dp@dp:~/cursestest % gcc -lncursesw 1.c -o mytest dp@dp:~/cursestest % ./mytest
5、窗口與子窗口
dp@dp:~/cursestest % cat 1.c
#include <ncurses.h>
#include <locale.h>
int main(){
//init_pair(short index,short foreground,short background)初始化顏色索引
//attron(COLOR_PAIR(索引號)|屬性)
//newwin建立窗口,derwin建立窗口的子窗口(相對于父窗口相對位置),subwin建立窗口的子窗口(相對于根窗口絕對位置)
setlocale(LC_ALL,"");
WINDOW *win1,*win2,*subwin;
initscr();//初始化
win1=newwin(15,50,1,1);//新窗口(行,列,begin_y,begin_x)
box(win1,ACS_VLINE,ACS_HLINE);
mvwprintw(win1,1,1,"WIN1");
mvwprintw(win1,2,1,"您好,很高興認(rèn)識您");
win2=newwin(10,40,10,30);//新窗口(行,列,begin_y,begin_x)
box(win2,ACS_VLINE,ACS_HLINE);
mvwprintw(win2,1,1,"WIN2");
mvwprintw(win2,2,1,"您好,很高興認(rèn)識您");
subwin=derwin(win2,3,20,3,5); //子窗口
box(subwin,ACS_VLINE,ACS_HLINE);
mvwprintw(subwin,1,5,"按任意鍵退出");//(窗口,y,x,字符串)
refresh();//刷新整個大窗口stdscr
wrefresh(win1);
wrefresh(win2);
touchwin(win1);//轉(zhuǎn)換當(dāng)前窗口為win1
wrefresh(win1);
getch();//win1顯示完,等待按鍵顯示win2
touchwin(win2);//轉(zhuǎn)換當(dāng)前窗口為win2
//使用doupdate,可以事先定義要刷新的部分,然后刷新
wnoutrefresh(win2);
wnoutrefresh(subwin);
doupdate();
getch();//等待按鍵
delwin(win1);
delwin(subwin);
delwin(win2);
endwin();//結(jié)束
return 0;
}
dp@dp:~/cursestest % gcc -lncursesw 1.c -o mytest
dp@dp:~/cursestest % ./mytest
6、自動滾屏
dp@dp:~/cursestest % cat 2.c
#include <ncurses.h>
#include <locale.h>
int main(void){
int y,x,i,j,h,w;
setlocale(LC_ALL,"");
WINDOW *pad;
initscr();//初始化
getmaxyx(stdscr,h,w);//獲得屏幕尺寸
//畫背景
for(i=0;i<h;i++){
for(j=0;j<w;j++){
mvaddch(i,j,ACS_CKBOARD);
}
}
refresh();
//建立窗口
pad=newpad(80,90);
for (i=0;i<80;i++){
char line[90];
sprintf(line,"line %d\n",i);
mvwprintw(pad,i,1,line);
}
refresh();
prefresh(pad,0,1,5,10,20,25);//刷新pad。0,1 為基墊需要顯示區(qū)域的左上角置(行列對,以下同此)。5,10,20,45為屏幕顯示區(qū)域的左上角和右下角位置
for(i=0;i<65;i++){
prefresh(pad,i+1,1,5,10,20,25);//刷新pad,實現(xiàn)流屏;
usleep(30000);
}
getch();//等待按鍵
delwin(pad);
endwin();//結(jié)束
return 0;
}
dp@dp:~/cursestest % gcc -lncursesw 2.c -o mytest
dp@dp:~/cursestest % ./mytest
7、在窗口中移動光標(biāo)
dp@dp:~/cursestest % cat 2.c
#include <ncurses.h>
#include <locale.h>
int main(void){
//init_pair(short index,short foreground,short background)初始化顏色索引
//attron(COLOR_PAIR(索引號)|屬性)
//newwin建立窗口,derwin建立窗口的子窗口(相對于父窗口相對位置),subwin建立窗的子窗口(相對于根窗口絕對位置)
int x,y;
setlocale(LC_ALL,"");
WINDOW *win1,*win2,*subwin;
initscr();//初始化
win1=newwin(15,50,1,1);//新窗口(行,列,begin_y,begin_x)
box(win1,ACS_VLINE,ACS_HLINE);
mvwprintw(win1,1,1,"WIN1");
mvwprintw(win1,2,1,"myhaspl@myhaspl.com");
win2=newwin(10,40,10,30);//新窗口(行,列,begin_y,begin_x)
box(win2,ACS_VLINE,ACS_HLINE);
wmove(win2,1,1);//移動某窗口的光標(biāo)
printw("WIN2");
wmove(win2,2,1);//移動某窗口的光標(biāo)。(窗口,y,x)
printw("myhaspl@myhaspl.com");
subwin=derwin(win2,3,20,4,5); //子窗口
box(subwin,ACS_VLINE,ACS_HLINE);
mvwprintw(subwin,1,5,"按任意鍵退出");//(窗口,y,x,字符串)
refresh();//刷新整個大窗口stdscr
wrefresh(win1);
wrefresh(win2);
move(5,60);//在stdscr移動光標(biāo)
printw("hello.........");
touchwin(win1);//轉(zhuǎn)換當(dāng)前窗口為win1
wrefresh(win1);
getch();//win1顯示完,等待按鍵顯示win2
touchwin(win2);//轉(zhuǎn)換當(dāng)前窗口為win2
//使用doupdate,可以事先定義要刷新的部分,然后刷新
wnoutrefresh(win2);
wnoutrefresh(subwin);
doupdate();
getyx(subwin,y,x);//獲得當(dāng)前邏輯光標(biāo)位置
mvwprintw(subwin,y+1,x,"................");//在“按任意鍵退出"下一行輸出"..............."
getch();//等待按鍵
delwin(win1);
delwin(subwin);
delwin(win2);
endwin();//結(jié)束
return 0;
}
編譯后運(yùn)行
dp@dp:~/cursestest % gcc -lncursesw 2.c -o mytest
dp@dp:~/cursestest % ./mytest
8、菜單
dp@dp:~/cursestest % cat 2.c
#include <locale.h>
#include <menu.h>
#include <stdio.h>
#include <ctype.h>
//定義菜單項
static const char *menus[]={
"1-1","1-2","1-3","2-1","2-2","2-3"
};
#define CITEM sizeof(menus)/sizeof(menus[0])//菜單項數(shù)
ITEM *items[CITEM];
int main(int argc,char *argv[]){
int i;
int ch;
int mrows,mcols;
WINDOW *win,*subwin;
MENU *mymenu;
//初始化屏幕
initscr();
//不用等待回車鍵
cbreak();
//不回顯
noecho();
//可以處理功能鍵
keypad(stdscr,TRUE);
//建立菜單項
for(i=0;i<CITEM;i++){
items[i]=new_item(menus[i],menus[i]);//第二個參數(shù)為菜單項的描述
}
//建立菜單
mymenu=new_menu(items);
set_menu_format(mymenu,CITEM,1); //設(shè)置CITEM行1列的菜單
set_menu_mark(mymenu,">");//菜單選中的MARK
//獲得菜單的行列數(shù)
scale_menu(mymenu,&mrows,&mcols);
//建立窗口和子窗口
win=newwin(mrows+2,mcols+2,3,30);
keypad(win,TRUE);
box(win,0,0);
subwin=derwin(win,0,0,1,1);
//設(shè)置菜單的窗口
set_menu_sub(mymenu,subwin);
//在子窗口上放置菜單
post_menu(mymenu);
refresh();
wrefresh(win);
//獲得輸入,并移動選擇到相應(yīng)的菜單項
while(toupper(ch=wgetch(win))!='\n'){
if(ch==KEY_DOWN)
menu_driver(mymenu,REQ_DOWN_ITEM);//移動菜單選擇
else if(ch==KEY_RIGHT)
menu_driver(mymenu,REQ_RIGHT_ITEM);
else if (ch==KEY_UP)
menu_driver(mymenu,REQ_UP_ITEM);
else if (ch==KEY_LEFT)
menu_driver(mymenu,REQ_LEFT_ITEM);
}
//輸出當(dāng)前項
mvprintw(LINES-2,0,"you select the item :%s\n",item_name(current_item(mymenu)));
refresh();
unpost_menu(mymenu);
getch();
//釋放內(nèi)存
free_menu(mymenu);
for(i=0;i<CITEM;i++) free_item(items[i]);
endwin();
return 1;
}
編譯并運(yùn)行
dp@dp:~/cursestest % gcc -lncursesw -lmenu 2.c -o mytest
dp@dp:~/cursestest % ./mytest
相關(guān)文章
C語言for循環(huán)嵌套for循環(huán)在實踐題目中應(yīng)用詳解
初學(xué)C語言,常常遇到for循環(huán)中嵌套個for循環(huán),初學(xué)者對于這種形式總是一知半解,這次我就整理了常見的for循環(huán)嵌套for循環(huán)的題目,我們一起爭取一舉拿下這類題。學(xué)廢他們,以后再見到就不怕啦!每天都要學(xué)一點(diǎn)呀。加油,奮斗的我們2022-05-05
C/C++ Crypto密碼庫調(diào)用的實現(xiàn)方法
Crypto 庫是C/C++的加密算法庫,這個加密庫很流行,基本上涵蓋了市面上的各類加密解密算法,感興趣的可以參考一下2021-06-06
C++如何解決rand()函數(shù)生成的隨機(jī)數(shù)每次都一樣的問題
這篇文章主要介紹了C++如何解決rand()函數(shù)生成的隨機(jī)數(shù)每次都一樣的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
C語言新手練習(xí)之多維數(shù)組、結(jié)構(gòu)體及函數(shù)
這篇文章主要給大家介紹了關(guān)于C語言新手練習(xí)之多維數(shù)組、結(jié)構(gòu)體及函數(shù)的相關(guān)資料,這些內(nèi)容在我們?nèi)粘i_發(fā)中經(jīng)常會遇到的,需要的朋友可以參考下2021-06-06

