服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C指针原理教程之Ncurses介绍

C指针原理教程之Ncurses介绍

2021-07-21 15:03myhaspl C/C++

Ncurses 提供字符终端处理库,包括面板和菜单。为了能够使用ncurses库,您必须在您的源程序中将curses.h包括(include)进来,而且在编译的需要与它连接起来. 在gcc中您可以使用参数-lcurses进行编译.

1、安装Ncurses

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

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

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

· 创建和管理一个窗口

· 使用8种不同的彩色

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

· 使用键盘上的功能键

Ubuntu下

?
1
2
3
mysea@mysea-desktop:~$ sudo apt-get install libncurses5-dbg libncurses5-dev
 
mysea@mysea-desktop:~/test$ gcc -lncurses -o cursestest cursestest.c

Freebsd下

?
1
2
3
cd /usr/ports/devel/ncurses-devel
 
make install clean

2、hello,world

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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; 
 
}

编译及运行

?
1
2
3
dp@dp:~/cursestest % gcc -lncurses 1.c -o mytest
 
dp@dp:~/cursestest % ./mytest

 3、色彩

然后编写下面代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#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、对中文的支持

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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库

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

5、窗口与子窗口

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#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、自动滚屏

dp@dp:~/cursestest % cat 2.c

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#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 为基垫需要显示区域的左上角置(行列对,以下同此)。5,10,20,45为屏幕显示区域的左上角和右下角位置
 
 
 
  for(i=0;i<65;i++){
 
    prefresh(pad,i+1,1,5,10,20,25);//刷新pad,实现流屏;
 
    usleep(30000);
 
  }
 
  getch();//等待按键
 
  delwin(pad);
 
  endwin();//结束
 
  return 0; 
 
}

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

dp@dp:~/cursestest % ./mytest

7、在窗口中移动光标

dp@dp:~/cursestest % cat 2.c

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#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);//移动某窗口的光标
 
  printw("WIN2");
 
  wmove(win2,2,1);//移动某窗口的光标。(窗口,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移动光标
 
  printw("hello.........");
 
  touchwin(win1);//转换当前窗口为win1
 
  wrefresh(win1);
 
  getch();//win1显示完,等待按键显示win2
 
  touchwin(win2);//转换当前窗口为win2
 
  //使用doupdate,可以事先定义要刷新的部分,然后刷新
 
  wnoutrefresh(win2); 
 
  wnoutrefresh(subwin);
 
  doupdate();
 
  getyx(subwin,y,x);//获得当前逻辑光标位置
 
  mvwprintw(subwin,y+1,x,"................");//在“按任意键退出"下一行输出"..............."
 
  getch();//等待按键
 
  delwin(win1);
 
  delwin(subwin);
 
  delwin(win2);
 
  endwin();//结束
 
  return 0; 
 
}

编译后运行

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

dp@dp:~/cursestest % ./mytest

8、菜单

dp@dp:~/cursestest % cat 2.c

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#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])//菜单项数
 
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]);//第二个参数为菜单项的描述
 
  }
 
  //建立菜单
 
  mymenu=new_menu(items);
 
  set_menu_format(mymenu,CITEM,1);  //设置CITEM行1列的菜单
 
  set_menu_mark(mymenu,">");//菜单选中的MARK
 
  //获得菜单的行列数
 
  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);
 
  //设置菜单的窗口
 
  set_menu_sub(mymenu,subwin); 
 
  //在子窗口上放置菜单
 
  post_menu(mymenu);
 
  
 
  refresh();
 
  wrefresh(win);
 
  //获得输入,并移动选择到相应的菜单项
 
  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);
 
  }
 
  //输出当前项
 
  mvprintw(LINES-2,0,"you select the item :%s\n",item_name(current_item(mymenu)));
 
  refresh();
 
  unpost_menu(mymenu);
 
  getch();
 
  //释放内存
 
  free_menu(mymenu);
 
  for(i=0;i<CITEM;i++) free_item(items[i]);
 
  endwin();
 
  return 1;
 
}

编译并运行

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

dp@dp:~/cursestest % ./mytest

原文链接:http://blog.51cto.com/13959448/2338456

延伸 · 阅读

精彩推荐
  • C/C++c/c++实现获取域名的IP地址

    c/c++实现获取域名的IP地址

    本文给大家汇总介绍了使用c/c++实现获取域名的IP地址的几种方法以及这些方法的核心函数gethostbyname的详细用法,非常的实用,有需要的小伙伴可以参考下...

    C++教程网10262021-03-16
  • C/C++C语言main函数的三种形式实例详解

    C语言main函数的三种形式实例详解

    这篇文章主要介绍了 C语言main函数的三种形式实例详解的相关资料,需要的朋友可以参考下...

    ieearth6912021-05-16
  • C/C++使用C++制作简单的web服务器(续)

    使用C++制作简单的web服务器(续)

    本文承接上文《使用C++制作简单的web服务器》,把web服务器做的功能稍微强大些,主要增加的功能是从文件中读取网页并返回给客户端,而不是把网页代码...

    C++教程网5492021-02-22
  • C/C++深入C++拷贝构造函数的总结详解

    深入C++拷贝构造函数的总结详解

    本篇文章是对C++中拷贝构造函数进行了总结与介绍。需要的朋友参考下...

    C++教程网5182020-11-30
  • C/C++c/c++内存分配大小实例讲解

    c/c++内存分配大小实例讲解

    在本篇文章里小编给大家整理了一篇关于c/c++内存分配大小实例讲解内容,有需要的朋友们可以跟着学习参考下。...

    jihite5172022-02-22
  • C/C++关于C语言中E-R图的详解

    关于C语言中E-R图的详解

    今天小编就为大家分享一篇关于关于C语言中E-R图的详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看...

    Struggler095962021-07-12
  • C/C++OpenCV实现拼接图像的简单方法

    OpenCV实现拼接图像的简单方法

    这篇文章主要为大家详细介绍了OpenCV实现拼接图像的简单方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    iteye_183805102021-07-29
  • C/C++C语言实现双人五子棋游戏

    C语言实现双人五子棋游戏

    这篇文章主要为大家详细介绍了C语言实现双人五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    两片空白7312021-11-12