#include #include #define ESC 27 #define ENTER 10
#define ITEM_NUM_1 3 #define ITEM_NUM_2 2 #define ITEM_NUM_3 3 #define ITEM_NUM_4 4
#define ITEM_NO_1 1 #define ITEM_NO_2 2 #define ITEM_NO_3 3 #define ITEM_NO_4 4
void init_curses(void); void Draw_menuhead(WINDOW *menuhead); WINDOW **Draw_menu(int start_cols,int itemNo); int view_menu(WINDOW **items,/*int itemNum,*/int start_cols,int itemNo); void Del_menu(WINDOW **items,int itemNum); int item_num(int itemNo);
main() { WINDOW *menuhead,*message; WINDOW **items;/*菜单项目指针,数组元素指向菜单项子项*/ int Key; int count;/*菜单子项的数目*/ int Re_item;/*菜单浏览返回值*/
init_curses();
wbkgd(stdscr,COLOR_PAIR(1)); menuhead=subwin(stdscr,1,80,0,0); message=subwin(stdscr,1,79,23,1);
Draw_menuhead(menuhead);/*绘制菜单头部*/ move(2,1); printw("Please F1--F4 Select Menu..."); move(3,1); printw("Please ESC Quit..."); do { Key=getch(); switch(Key) { case KEY_F(1): items=Draw_menu(3,ITEM_NO_1);/*第3列,第1菜单*/ Re_item=view_menu(items,/*ITEM_NUM_1,*/3,ITEM_NO_1); touchwin(stdscr); refresh(); break; case KEY_F(2): items=Draw_menu(23,ITEM_NO_2);/*第23列,第2菜单*/ Re_item=view_menu(items,/*ITEM_NUM_2,*/23,ITEM_NO_2); touchwin(stdscr); refresh(); break;
case KEY_F(3): items=Draw_menu(43,ITEM_NO_3);/*第43列,第3菜单*/ Re_item=view_menu(items,/*ITEM_NUM_3,*/43,ITEM_NO_3); touchwin(stdscr); refresh(); break; case KEY_F(4): items=Draw_menu(63,ITEM_NO_4);/*第63列,第4菜单*/ Re_item=view_menu(items,/*ITEM_NUM_4,*/63,ITEM_NO_4); touchwin(stdscr); refresh(); break; default: break; } } while(Key!=ESC);
delwin(message); delwin(menuhead); endwin(); exit(0); }
void Draw_menuhead(WINDOW *menuhead) { wbkgd(menuhead,COLOR_PAIR(2)); wrefresh(menuhead); wmove(menuhead,0,0); waddstr(menuhead,"菜单1"); wattron(menuhead,COLOR_PAIR(3)); waddstr(menuhead,"(F1)"); wattroff(menuhead,COLOR_PAIR(3)); wmove(menuhead,0,20); waddstr(menuhead,"菜单2"); wattron(menuhead,COLOR_PAIR(3)); waddstr(menuhead,"(F2)"); wattroff(menuhead,COLOR_PAIR(3)); wmove(menuhead,0,40); waddstr(menuhead,"菜单3"); wattron(menuhead,COLOR_PAIR(3)); waddstr(menuhead,"F(3)"); wattroff(menuhead,COLOR_PAIR(3)); wmove(menuhead,0,60); waddstr(menuhead,"菜单4"); wattron(menuhead,COLOR_PAIR(3)); waddstr(menuhead,"F(4)"); wattroff(menuhead,COLOR_PAIR(3)); }
void init_curses(void) { initscr(); noecho(); keypad(stdscr,TRUE); /*intrflush(stdscr,FALSE);*/ start_color(); /*初始化*/ init_pair(1,COLOR_WHITE,COLOR_BLUE); init_pair(2,COLOR_BLUE,COLOR_WHITE); init_pair(3,COLOR_RED,COLOR_WHITE);
curs_set(0); /*设置光标不可见*/ }
WINDOW **Draw_menu(int start_cols,int itemNo) { WINDOW **items;
switch(itemNo) { case 1: items=(WINDOW **)malloc((ITEM_NUM_1+1)*sizeof(WINDOW *));
items[0]=newwin(ITEM_NUM_1+2,19,1,start_cols); wbkgd(items[0],COLOR_PAIR(2)); box(items[0],ACS_VLINE,ACS_HLINE);
items[1]=subwin(items[0],1,17,2,start_cols+1); wprintw(items[1],"子项A1"); items[2]=subwin(items[0],1,17,3,start_cols+1); wprintw(items[2],"子项A2"); items[3]=subwin(items[0],1,17,4,start_cols+1); wprintw(items[3],"子项A3"); /* items[4]=subwin(items[0],1,17,5,start_cols+1); wprintw(items[4],"子项A4");*/ break; case 2: items=(WINDOW **)malloc((ITEM_NUM_2+1)*sizeof(WINDOW *));
items[0]=newwin(ITEM_NUM_2+2,19,1,start_cols); wbkgd(items[0],COLOR_PAIR(2)); box(items[0],ACS_VLINE,ACS_HLINE);
items[1]=subwin(items[0],1,17,2,start_cols+1); wprintw(items[1],"子项B1"); items[2]=subwin(items[0],1,17,3,start_cols+1); wprintw(items[2],"子项B2"); /* items[3]=subwin(items[0],1,17,4,start_cols+1); wprintw(items[3],"子项B3"); items[4]=subwin(items[0],1,17,5,start_cols+1); wprintw(items[4],"子项B4");*/ break; case 3: items=(WINDOW **)malloc((ITEM_NUM_3+1)*sizeof(WINDOW *));
items[0]=newwin(ITEM_NUM_3+2,19,1,start_cols); wbkgd(items[0],COLOR_PAIR(2)); box(items[0],ACS_VLINE,ACS_HLINE);
items[1]=subwin(items[0],1,17,2,start_cols+1); wprintw(items[1],"子项C1"); items[2]=subwin(items[0],1,17,3,start_cols+1); wprintw(items[2],"子项C2"); items[3]=subwin(items[0],1,17,4,start_cols+1); wprintw(items[3],"子项C3"); /* items[4]=subwin(items[0],1,17,5,start_cols+1); wprintw(items[4],"子项C4");*/ break; case 4: items=(WINDOW **)malloc((ITEM_NUM_4+1)*sizeof(WINDOW *));
items[0]=newwin(ITEM_NUM_4+2,19,1,start_cols); wbkgd(items[0],COLOR_PAIR(2)); box(items[0],ACS_VLINE,ACS_HLINE);
items[1]=subwin(items[0],1,17,2,start_cols+1); wprintw(items[1],"子项D1"); items[2]=subwin(items[0],1,17,3,start_cols+1); wprintw(items[2],"子项D2"); items[3]=subwin(items[0],1,17,4,start_cols+1); wprintw(items[3],"子项D3"); items[4]=subwin(items[0],1,17,5,start_cols+1); wprintw(items[4],"子项D4"); break; default: break; } wbkgd(items[1],COLOR_PAIR(1)); wrefresh(items[0]); return items; }
view_menu(WINDOW **items,/*int itemNum,*/int start_cols,int itemNo) { int select=0;/*适应求余算法,0--itemNum-1,对应1--itemNum*/ int Key; int itemNum;
itemNo=itemNo-1;/*适应求余算法,0--3对应1--4*/ wrefresh(items[0]); A1: itemNum=item_num(itemNo+1); while(1) { Key=getch(); if(Key==KEY_DOWN || Key==KEY_UP) { wbkgd(items[select+1],COLOR_PAIR(2)); wnoutrefresh(items[select+1]); if (Key==KEY_DOWN) select=(select+1) % itemNum; else select=(select+itemNum-1) % itemNum; wbkgd(items[select+1],COLOR_PAIR(1)); wnoutrefresh(items[select+1]);/*select+1为第几项目1/2/3../itemNum*/ doupdate(); } else { if (Key==KEY_LEFT || Key==KEY_RIGHT) { Del_menu(items,itemNum+1); touchwin(stdscr); refresh(); if (Key==KEY_RIGHT) if (start_cols<63){ itemNo=(itemNo+1) % 4; /*4: 一共4个菜单*/ start_cols=start_cols+20; /*20: 菜单之间原点间隔数*/ } else{ itemNo=0; start_cols=3; } else if (start_cols>3) { itemNo=(itemNo+4-1) % 4; start_cols=start_cols-20; } else { itemNo=3; start_cols=63; }
items=Draw_menu(start_cols,itemNo+1);/*itemNo+1为菜单号1\2\3\4*/ select=0;
goto A1; /*return view_menu(items,4,start_cols); */ } if (Key==ESC) return -1; else if (Key==ENTER){ return 1; /* */ } } } }
void Del_menu(WINDOW **items,int itemNum) { int i;
for (i=0;i delwin(items); free(items); }
item_num(int itemNo)/*根据菜单号找出对应项目数*/ { switch(itemNo) { case 1: return ITEM_NUM_1; case 2: return ITEM_NUM_2; case 3: return ITEM_NUM_3; case 4: return ITEM_NUM_4; }
| | |