1. 程式人生 > >C指標原理(29)-Ncurses-文字終端的圖形

C指標原理(29)-Ncurses-文字終端的圖形

1、安裝Ncurses

Ncurses是一個能提供功能鍵定義(快捷鍵),螢幕繪製以及基於文字終端的圖形互動功能的動態庫。

Ncurses是一個能提供基於文字終端視窗功能的動態庫. Ncurses可以:

· 只要您喜歡,您可以使用整個螢幕

· 建立和管理一個視窗

· 使用8種不同的彩色

· 為您的程式提供滑鼠支援

· 使用鍵盤上的功能鍵

Ubuntu下

[email protected]:~$ sudo apt-get install libncurses5-dbg libncurses5-dev

[email protected]:~/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();//結束

    return 0;    

}

編譯及執行

[email protected]:~/cursestest % gcc -lncurses 1.c -o mytest

[email protected]:~/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();//結束

    

執行

4、對中文的支援

[email protected]:~/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();//結束

    return 0;    

}

編譯時注意要使用ncursesw庫,不使用ncurses庫

[email protected]:~/cursestest % gcc -lncursesw 1.c -o mytest

[email protected]:~/cursestest % ./mytest

執行效果 :

5、視窗與子視窗

[email protected]:~/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,"您好,很高興認識您");
    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,"您好,很高興認識您");
    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);//轉換當前視窗為win1
    wrefresh(win1);
    getch();//win1顯示完,等待按鍵顯示win2
    touchwin(win2);//轉換當前視窗為win2 
    //使用doupdate,可以事先定義要重新整理的部分,然後重新整理
    wnoutrefresh(win2);  
    wnoutrefresh(subwin);
    doupdate();
    getch();//等待按鍵
    delwin(win1);
    delwin(subwin);
    delwin(win2);
    endwin();//結束
    return 0;   
}

[email protected]:~/cursestest % gcc -lncursesw 1.c -o mytest
[email protected]:~/cursestest % ./mytest