- 论坛徽章:
- 0
|
#include <curses.h>;
WINDOW *msgbox;
struct pop_struc
{
char *name;
char *msg;
int (*fun)();
int select_id;
};
int submenu(int y, int x);
int pop_up(struct pop_struc pop_menu[],int y, int x);
main()
{
struct pop_struc pop_menu[6]={
/* *name *msg (*fun)() select_id */
" 排 序 ", " 按照一定方式排序! ", submenu, 0,
" 打 印 ", " 打印子菜单。 ", submenu, 0,
" 删 除 ", " 删除当前所选项目。 ", submenu, 0,
" 复 制 ", " 将当前内容复制到磁盘。 ", submenu, 0,
"退出系统 ", " 退出当前系统 ", submenu, 0,
'\0'
};
int y=0;
int x=0;
char *str;
int i;
initscr();
cbreak();
nonl();
noecho();
intrflush(stdscr,FALSE);
keypad(stdscr,TRUE);
curs_set(TRUE);
msgbox=newwin(1,80,23,0);
wattron(msgbox,A_REVERSE);
str="状态栏: ";
mvwaddstr(msgbox,0,0,str);
i = pop_up(pop_menu,y,x);
endwin();
}
int pop_up(struct pop_struc pop_menu[],int y, int x)
{
WINDOW *test;
int nu_opt,i;
int width,cur_opt,col;
int ch;
i=0;
nu_opt=0;
/* 获取菜单项数 */
while (pop_menu[i++].name!='\0');
nu_opt=i-1;
width=0;
/* 获取最长菜单项长度 */
for (i=0; i<nu_opt; i++)
{
if (strlen(pop_menu.name) >; width )
{
width= strlen(pop_menu.name);
}
}
test= newwin(nu_opt+2,width+2,y,x);
keypad(test,TRUE);
box(test,'│','─');
wrefresh(test);
cur_opt=0 ;
for (;![](static/image/smiley/default/icon_wink.gif)
{
col=1;
for (i=0; i< 5; i++)
{
if (i == cur_opt) wattron(test,A_REVERSE);
else
{
y = test->;_begy+cur_opt+1;
x = test->;_begx+width;
wattroff(test,A_REVERSE);
}
mvwaddstr(test,col++,1,pop_menu.name);
}
mvwaddstr(msgbox,0,9,pop_menu[cur_opt].msg);
wrefresh(msgbox);
ch=wgetch(test);
switch(ch)
{
case KEY_DOWN:
cur_opt++;
if (cur_opt >;= nu_opt) cur_opt = 0;
break;
case KEY_UP:
cur_opt--;
if (cur_opt < 0) cur_opt = nu_opt-1;
break;
case 27:
endwin();
return 0;
case '\r':
i=(*pop_menu[cur_opt].fun)(y,x);
touchwin(test);
wrefresh(test);
break;
default:
break;
}
}
}
int submenu(int y, int x)
{
int i;
int ch;
WINDOW *submenu;
if (y >; 23 ) y=23;
if (x >; 79 ) x=79;
if (y < 0 ) y=0;
if (x < 0 ) x=0;
submenu = newwin(10,30,y,x);
keypad(submenu, TRUE);
box(submenu,'┃','─');
wattron(submenu,A_REVERSE);
mvwaddstr(submenu,0,5,"请按上、下方向键" ;
wattroff(submenu,A_REVERSE);
touchwin(submenu);
wrefresh(submenu);
for (;![](static/image/smiley/default/icon_wink.gif)
{
ch = wgetch(submenu);
switch(ch)
{
case 27:
werase(submenu);
wrefresh(submenu);
delwin(submenu);
return 0;
case KEY_DOWN:
case KEY_UP:
mvwaddstr(submenu,3,4,"模拟一个子菜单" ;
break;
}
}
} |
|