C指针原理教程之Ncurses介绍分享!

1、安装Ncurses

Ncurses是一个能提供功能键定义(快捷键),屏幕绘制以及基于文本终端的图形互动功能的动态库。

Ncurses是一个能提供基于文本终端窗口功能的动态库. Ncurses可以:

· 只要您喜欢,您可以使用整个屏幕

· 创建和管理一个窗口

· 使用8种不同的彩色

· 为您的程序提供鼠标支持

· 使用键盘上的功能键

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();//结束      return 0;      }    

编译及运行

  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();//结束        

执行

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();//结束      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,"您好,很高兴认识您");    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;    }  

dp@dp:~/cursestest % gcc -lncursesw 1.c -o mytest
dp@dp:~/cursestest % ./mytest

6、自动滚屏

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/482785.html

(0)
上一篇 2020年11月9日
下一篇 2020年11月9日

精彩推荐