C指针原理教程之Ncurses介绍

  

<强> 1,安装Ncurses

  

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

  

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

  

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

  

·创建和管理一个窗口

  

·使用8种不同的彩色

  

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

  

·使用键盘上的功能键

  Ubuntu下

        mysea@mysea-desktop: ~ $ sudo apt-get安装libncurses5-dbg libncurses5-dev      mysea@mysea-desktop: ~/测试gcc -lncurses美元- o cursestest cursestest.c      之前      

Freebsd下         cd/usr/ports/devel/ncurses-devel      使安装清洁      之前      

2, hello, world

        # include & lt; curses.h>      int主要(空白){      initscr();//初始化      箱(stdscr ACS_VLINE ACS_HLINE);//画边框      mvaddstr (15 2“hello, world”);//在15日2显示字符串      refresh();//刷新屏幕      getch();//等待按键      endwin();//结束      返回0;      }      之前      

编译及运行

        dp@dp: ~/cursestest % gcc -lncurses 1。c - o mytest      dp@dp: ~//mytest cursestest %      之前      

, 3色彩

  

然后编写下面代码:

        # include & lt; ncurses.h>      # include & lt; locale.h>      # include & lt; stdio.h>      int主要(空白){//init_pair(简短指数、前景、背景)初始化颜色索引//attron (COLOR_PAIR(索引号)|属性)      setlocale (LC_ALL”、“);      initscr();//初始化      箱(stdscr ACS_VLINE ACS_HLINE);//画边框      如果(! has_colors () | | start_color()==犯错){      endwin ();      printf("终端不支持颜色\ n”);      返回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;      (i=1; i<=3;我+ +){      attron (COLOR_PAIR (i));      (我,10);      printw (“hello, world: % d”,我);      }      (i=1; i<=3;我+ +){      attron (COLOR_PAIR (i) | A_UNDERLINE);      (我+ 5,10);      printw (“hello, world: % d”,我);      }      refresh();//刷新屏幕      getch();//等待按键      endwin();//结束         之前      

执行   

4,对中文的支持

        猫dp@dp: ~/cursestest % 1.摄氏度         # include & lt; ncurses.h>      # include & lt; locale.h>      # include & lt; stdio.h>      int主要(空白){//init_pair(简短指数、前景、背景)初始化颜色索引//attron (COLOR_PAIR(索引号)|属性)      setlocale (LC_ALL”、“);      initscr();//初始化      箱(stdscr ACS_VLINE ACS_HLINE);//画边框      如果(! has_colors () | | start_color()==犯错){      endwin ();      printf("终端不支持颜色\ n”);      返回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;      (i=1; i<=3;我+ +){      attron (COLOR_PAIR (i));      (我,10);      printw(“你好,世界% d ", 1);      }      (i=1; i<=3;我+ +){      attron (COLOR_PAIR (i) | A_UNDERLINE);      (我+ 5,10);      printw(“你好,世界:% d ", 1);      }      refresh();//刷新屏幕      getch();//等待按键      endwin();//结束      返回0;      }      之前      

编译时注意要使用ncursesw库,不使用ncurses库

        dp@dp: ~/cursestest % gcc -lncursesw 1。c - o mytest      dp@dp: ~//mytest cursestest %      之前      

5,窗口与子窗口

  

dp@dp:猫~/cursestest % 1. c

        # include & lt; ncurses.h>   # include & lt; locale.h>   int main () {//init_pair(简短指数、前景、背景)初始化颜色索引//attron (COLOR_PAIR(索引号)|属性)//newwin建立窗口,derwin建立窗口的子窗口(相对于父窗口相对位置),subwin建立窗口的子窗口(相对于根窗口绝对位置)   setlocale (LC_ALL”、“);   窗口* win1, * win2 * subwin;   initscr();//初始化   win1=newwin(15岁,50岁,1,1);//新窗口(行、列、begin_y begin_x)   箱(win1 ACS_VLINE ACS_HLINE);   mvwprintw (win1, 1, 1,“win1”);   mvwprintw (win1 2 1,“您好,很高兴认识您”);   win2=newwin(10, 40岁,10、30);//新窗口(行、列、begin_y begin_x)   箱(win2 ACS_VLINE ACS_HLINE);   mvwprintw (win2, 1, 1,“win2”);   mvwprintw (win2 2 1,“您好,很高兴认识您”);   subwin=derwin (20 win2 3、3、5);//子窗口   箱(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();//等待按键   戴尔文(win1);   戴尔文(subwin);   戴尔文(win2);   endwin();//结束   返回0;   }   

C指针原理教程之Ncurses介绍