
COMPUTER ENGINEERING 
1999 25 5 Vol.25 No.5 1999



һӱĲ˵Ƽ

ժҪ һȼӦ˵ƼҪ˼뼰ʵԭЧʸߣ۴󷽵ص㡣
ؼ ˵ ȼ Borland C++3.1
A New Technique for Designing Menu
Shao Ming
Qingdao Architecture Engineering InstituteMechanical and Electrical Engineering Department Qingdao 266520
[Abstract] This essay introduces the designing of pull-dowm menu with hot keys to respond to and puts emphasis on ideas of how to disign it and principles of how to make it work It has characteristics of high efficiencypleasing to the eye and so on
[Key words] Pull-dowm menuHot keyBorland C++3.1
Ӧó,ûȽӴľӦóеĲ˵ϵͳ,˵ϵͳƺûӳӦóеĹģ֯ˮƽ,ͬʱҲӳӦóûԡĿǰ˵Ƽֶ֧Բ˵ѡ:һʹüһ˵صĸּ,Ϊȼ;һùλ豸˵ƶָ˵»س갴ťȼӦֻڰò˵Ĳ˵ڻڼſʹ,ӦӲ˵еȼ,һ̶Ӱ˵ѡִЧʡʵӦϵͳƵĶ༶ʽ˵,ʱӦͬӲ˵еȼ,߲˵ִЧʡ
1 ̶ʽ˵
˵ϵͳõǹ̶ʽ˵,һָ˵̶ʾĻ,Ӳ˵ʹʱĻϵһ˵һӦóϵͳҪʹһͼ1ʾĶ˵ÿһ˵Բ˵ѡõѡ񷽷֮,һЩò˵һЩȼ,۵ǰ˵괦һӲ˵,ɲͨ˵ѡ,ֱӰȼѡò˵ڱ˵ϵͳȼ

ͼ1 ˵ʾͼ
ALT_FALT_EALT_Gֱѡ˵ĸ˵;ALT_X,F2,F3ֱѡFileӲ˵еQuit,Save,LoadʵȼĴ,ǲ˵ƵĹؼ
2 ˵Ƽ
2.1 ƺݽṹ
Ϊһ˵,뾫֯һݽṹ,ݰ˵,˵ʾ,˵λúʹС,˵ѡԵȡͼ1˵ṹ߽ƣ
(1) ƴÿ˵ϢĽṹ
struct screen{
unsigned char left,top,right,bottom;/*˵ʾϽ,½*/
char bar_position;*ǰ˵Ӳ˵е*
char **menu_message;/*˵*/
char menu_attr;*˵*/
char commands;*˵ȼַ*
int screen_buff;};*¼˵ĻϢĻַ*/
ÿ˵£
struct screen main_menu={0,0,79,0,0,prompt0,3,"FEG",NULL},
file_menu={1,1,14,8,0,file_prompt,6,"LSWUOQ",NULL},
edit_menu={12,1,25,6,0,edit_prompt,3,"UEC",NULL},
graph_menu={21,1,34,7,0,graph_prompt,4,"LMBC",NULL},
null_screen={0,0,0,0,0,NULL,0,NULL,NULL};
ÿһӲ˵ʾϢΪ
char *prompt0[]={"File","Edit","Grapher"};
char *file_prompt[]={"Load F2","Save F3","Write to","Quit Alt_x"};
char *edit_prompt[]={"Undo","Erase","Copy"};
char *graph_prompt[]={"Line","Move","Box","Circle"};
(2)½ṹÿһ˵
struct node{int up,down,left,right;
/*¼˵Ӳ˵һ˵˵һһ˵ڽṹλ*/
struct screen *owner_screen;/*ò˵¼Ӳ˵Ϣ*
int(*sub_function)();};*ѡò˵ʱӦִеĺ*
һṹ,ÿһԪشһ˵Ϣ:
struct node nodes[]={{-1,1,-1,-1,&main_menu,select_subtask},
/*1 File*/{0,4,3,2,&file_menu,select_subtask},
/*2 Edit*/{0,8,1,3,&edit_menu,select_subtask},
/*3 Grapher*/{0,11,2,1,&graph_menu,select_subtask},
/*4 Load*/{1,-1,7,5,&null_screen,load_file},
/*5 Save*/{1,-1,4,6,&null_screen,save_file},
/*6 Write to*/{1,-1,5,7,&null_screen,NULL},
/*7 Quit*/{1,-1,6,4,&null_screen,exit_system},
/*8 Undo*/{2,-1,10,9,&null_screen,NULL},
/*9 Erase*/{2,-1,8,10,&null_screen,exit_system},
/*10 Copy*/{2,-1,9,8,&null_screen,NULL},
/*11 Line*/{3,-1,14,12,&null_screen,NULL},
/*12 Move*/{3,-1,11,13,&null_screen,NULL},
/*13 Box*/{3,-1,12,14,&null_screen,NULL},
/*14 Circle*/{3,-1,13,11,&null_screen,NULL}};
ĳ˵¼Ӳ˵,ѡò˵ʱִеĺǲ˵select_subtask(),úû,ǹƶ,Ѳ˵ӦĲ˵,Ϊѡ,򷵻ز˵ڲ˵е,ESC,ֵΪ-1ִиò˵غ,ЩӦ÷ֵΪ-1,Աɸùܺ˵һ˵
(3)һջ¼˵ѡ·,ӦĳһȼEscʱָÿһӲ˵ǵ
struct screen *screen_stackc[20],*current_menu;/*ջǰ˵*/
int screen_sp=-1,current_node;/*ջָ뼰ǰ˵ڽṹеλ*/
2.2 ҪƼ
(1)
main()
{int ret_code,i,j;
. . . . . ./*ɳʼ*/
setjmp(ebuf);/*úת*/
do{ current_menu=nodes[current_node].owner_screen;
push_screen(current_menu);
ret_code=nodes[current_node].sub_function();
if(ret_code==-1){pop_screen(current_menu);
current_node=nodes[current_node].up;}
else current_node=nodes[current_node].down+ret_code;
}while(1);}
(2)ѹջ:ѵǰӲ˵Ϣѹջ,ָλʾӲ˵
void push_screen(struct screen *sc)
{int i,j,mode;
for(j=0;j<=screen_sp;j++) if(sc==screen_stack[j]) return;
screen_sp++;screen_stack[screen_sp]=sc;
if(sc==&null_screen) return;
mode=(sc==&main_menu)?1:0; 
disp_menu(sc,mode);}/*ʾӲ˵,˵̶Ļһ,˵ֱʾ*/
(3)ջ:һ˵,һָ˵ĸĻϢ
void pop_screen(struct screen *sc)
{screen_sp--;
if(sc==&null_screen) return;
if(sc==&main_menu){draw_cursor_bar(&main_menu,main_menu.bar_position,0);
return;}/*˵ֻԹ*/
puttext(sc->left+1,sc->top+1,sc->right+2,sc->bottom+2,sc->screen_buffer);
free(sc->screen_buffer);}
(4)ֵΪȼȼ 
int get_key(void)
{int temp;
in.h.ah=0x00; int86(0x16,&in,&out);
temp=(out.h.al==0)?out.h.ah+256:out.h.al;
switch(temp){
case F2: case F3: case ALT_F:case ALT_E:case ALT_G:
case ALT_X:break;
case CURSOR_LEFT:
case CURSOR_RIGHT:if(current_node<=3&&current_node>=1)break;
else return(temp);
default:return(temp);}
hot_key=temp;hot_key_handle();}
˵Ϊʱ,ҹһ˵;Ϊȼ,丸˵һ˵Ӳ˵
(5)ȼ
int hot_key_handle()
{int i,j,temp[10],temp_sp,father,hot_node;
switch(hot_key){
case F2:hot_node=5;break;
case F3:hot_node=4;break;
case CURSOR_LEFT:hot_node=nodes[current_node].left;break;
case CURSOR_RIGHT:hot_node=nodes[current_node].right;break;
case ALT_F:hot_node=1;break;
case ALT_E:hot_node=2;break;
case ALT_G:hot_node=3;break;
case ALT_X:hot_node=9;break;} /*switch*/
/*ָѡ·¸Ӳ˵*/
for(j=screen_sp;j>=0;j--) pop_screen(screen_stack[j]);
screen_sp=-1; current_node=0;
/*temp¼ȼӦĲ˵˵Ӧ˵ڸԲ˵е*/
for(temp_sp=0,j=hot_node;j!=current_node;){
father=nodes[j].up; temp[temp_sp++]=j-nodes[father].down;
j=father;} temp_sp--;
/*ȼѡ·ִеȼӦ˵*/
for(j=temp_sp;j>=0;j--){
nodes[current_node].owner_screen->bar_position=temp[j];
push_screen(nodes[current_node].owner_screen);
current_node=nodes[current_node].down+temp[j];}
longjmp(ebuf,1);} /*򴦣ִȼӦ*/
3 
ƪޣֻܶ˵ɼҪƸε˵ֻڽṹټÿ˵еĸ˵ϢɣCԱдЧʸߣִٶȿ졣
߼飺ŮŮ35꣬ʦݿ⼰Ӧã
ߵλൺѧԺϵ ൺ 266520
ոڣ1998-12-02 ޻ڣ1999-01-21
