我也发段TC下的菜单控制代码,希望对大家有帮助
#include<stdio.h>#include <conio.h>
void windows(int left, int top, int right, int bottom,int background,int color) /*画窗口*/
{
window(left,top,right,bottom); /*设置位置和大小*/
textbackground(background); /*设置背景*/
textcolor(color); /*设置字体颜色*/
clrscr();
}
/*menu显示的菜单,index有几项菜单,left,top,right,bottom菜单在屏目的位置,background背景,color字体颜色*/
int SelectMenu(char *menu[],int index,int left, int top, int right, int bottom,int background,int color) /*返回用户的选择*/
{
int i,key,key1=1,p1=0; /*key1 按下了键盘的哪个键*/
while(key1!=13) /*按下回车结束*/
{
windows(left,top,right,bottom,background,color);/*画窗口*/
gotoxy(1,index+2);
key=(p1==index-1)?0:p1+1;
cprintf("Please Make a Choices[0-4]:%d",key); /*提示*/
gotoxy(1,2);
for(i=0;i<index;i++)
{
if(i==p1) textcolor(10);/*当前选中的菜单字体颜色*/
else textcolor(7); /*未选中的字体颜色*/
gotoxy(1,i+1);
cprintf("%s",menu[i]);
}
gotoxy(28,index+2);
key1=getch();
if(key1==72) p1=(p1==0)?index-1:--p1;
else if(key1==27) return -1; /*按下ESC返回-1*/
else if(key1==80) p1=(p1==index-1)?0:++p1;
}
return p1;
}
int main()
{
char *menu[]={"1.select","2.select","3.select","4.exit"};
int select;
select=SelectMenu(menu,4,10,5,50,10,0,15);
clrscr();
printf("You chose:%s",menu[select]);
getch();
return 0;
}