即有:
菜单1 菜单2 菜单3 |
主菜单 |
执行菜单X |
当按上下键时,在菜单1--3之间切换
按退格键时,回主菜单
在菜单1--3某项时按回车,进入执行。
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <graphics.h>
#define MAIN_MUNE_NUM 3
#define NAME_MAX_LAN 20
#define SCR_X 2
#define SCR_Y 5
#define WIN_X 5
#define WIN_Y 40
#define SUB_MUNE_H 20
#define TEXT_COLOR 3
#define ESC 283
#define RIGHT 19712
#define LEFT 19200
#define DOWN 20480
#define UP 18432
#define ENTER 7181
char *mainMune[MAIN_MUNE_NUM] =
{"Main mune 1"
,"Main mune 2"
,"Main mune 3"
} ;
char **subMune[MAIN_MUNE_NUM] ;
int subNum[MAIN_MUNE_NUM] = {2, 3, 3} ;
int chooseMain = 0, chooseSub = 0 ;
int subSwitch = 0 ;
float mMLen ;
void *muneBKView ;
int Init() ;
int InitGraph() ;
int InitMune() ;
int Process() ;
int drawBKView() ;
int drawMainMune(int num, int Mode) ;
int drawSubView(int num) ;
int drawSubMune(int num, int choose, int Mode) ;
int Exit(int Mode) ;
int main(void)
{
Init() ;
Process() ;
Exit(0) ;
}
int Init()
{
InitMune() ;
InitGraph() ;
mMLen = (float)(getmaxx()-20)/MAIN_MUNE_NUM ;
settextstyle(1, 0, 5) ;
muneBKView = malloc(imagesize(SCR_X+5
, SCR_Y+25
, SCR_X+mMLen-5
, SCR_Y+25+subNum[0]*SUB_MUNE_H)
) ;
getimage(SCR_X+5, SCR_Y+25
, SCR_X+mMLen-5, SCR_Y+25+subNum[0]*SUB_MUNE_H
, muneBKView) ;
}
int InitGraph()
{
int gd = DETECT, gm = 0 ;
initgraph(&gd, &gm, "") ;
}
int InitMune()
{
int i, j ;
char *c = "Sub mune 0" ;
for(i = 0;i < MAIN_MUNE_NUM;i++)
{
subMune[i] = (char **)calloc(subNum[i],sizeof(char)) ;
}
for(i = 0;i < MAIN_MUNE_NUM;i++)
for(j = 0;j < subNum[i];j++)
{
subMune[i][j] = (char *)calloc(NAME_MAX_LAN,sizeof(char)) ;
}
strcpy(subMune[0][0], "Sub mune 0") ;
strcpy(subMune[0][1], "Sub mune 1") ;
strcpy(subMune[1][0], "Sub mune 0") ;
strcpy(subMune[1][1], "Sub mune 1") ;
strcpy(subMune[1][2], "Sub mune 2") ;
strcpy(subMune[2][0], "Sub mune 0") ;
strcpy(subMune[2][1], "Sub mune 1") ;
strcpy(subMune[2][2], "Sub mune 2") ;
}
int Process()
{
int key ;
drawBKView() ;
while(1)
{
if(kbhit())
{
key = bioskey(0) ;
switch(key)
{
case RIGHT:
if(subSwitch == 1)
{
putimage(SCR_X+chooseMain*mMLen+5, SCR_Y+25, muneBKView, COPY_PUT) ;
chooseSub = 0 ;
}
drawMainMune(chooseMain, 0) ;
chooseMain = (chooseMain > MAIN_MUNE_NUM-2) ? 0:chooseMain+1;
if(subSwitch == 1)
drawSubView(chooseMain) ;
drawMainMune(chooseMain, 1) ;
break ;
case LEFT:
if(subSwitch == 1)
{
putimage(SCR_X+chooseMain*mMLen+5, SCR_Y+25, muneBKView, COPY_PUT) ;
chooseSub = 0 ;
}
drawMainMune(chooseMain, 0) ;
chooseMain = (chooseMain < 1) ? MAIN_MUNE_NUM-1:chooseMain-1;
if(subSwitch == 1)
drawSubView(chooseMain) ;
drawMainMune(chooseMain, 1) ;
break ;
case DOWN:
if(subSwitch == 1)
{
drawSubMune(chooseMain, chooseSub, 0) ;
chooseSub = (chooseSub > subNum[chooseMain]-2) ? 0:chooseSub+1;
drawSubMune(chooseMain, chooseSub, 1) ;
}
else
{
chooseSub = 0 ;
subSwitch = 1 ;
drawSubView(chooseMain) ;
}
break ;
case UP:
if(subSwitch == 1)
{
drawSubMune(chooseMain, chooseSub, 0) ;
chooseSub = (chooseSub < 1) ? subNum[chooseMain]-1:chooseSub-1;
drawSubMune(chooseMain, chooseSub, 1) ;
}
break ;
case ESC:
if(subSwitch == 1)
putimage(SCR_X+chooseMain*mMLen+5, SCR_Y+25, muneBKView, COPY_PUT) ;
subSwitch = 0 ;
break ;
case ENTER:
Exit(0) ;
default :
sound(1000) ;
delay(100) ;
nosound() ;
break ;
}
}
}
getch() ;
}
int drawBKView()
{
int i ;
setcolor(1) ;
setfillstyle(1, 9) ;
bar3d(SCR_X, SCR_Y, getmaxx()-SCR_Y, getmaxy()-SCR_X, 3, 1) ;
setfillstyle(1, 5) ;
floodfill(SCR_X+10, SCR_Y-2, getcolor()) ;
floodfill(getmaxx()-SCR_Y+2, SCR_Y, getcolor()) ;
for(i = 0;i < MAIN_MUNE_NUM;i++)
drawMainMune(i, i == 0) ;
setcolor(6) ;
setfillstyle(1, 15) ;
bar3d(WIN_X, WIN_Y, getmaxx()-WIN_X-SCR_Y, getmaxy()-WIN_X, 3, 1) ;
setfillstyle(1, 10) ;
floodfill(WIN_X+10, WIN_Y-1, getcolor()) ;
floodfill(getmaxx()-WIN_X-SCR_Y+1, WIN_Y+1, getcolor()) ;
}
int drawMainMune(int num, int Mode)
{
setfillstyle(1, 6+Mode*4) ;
setcolor(4) ;
rectangle(SCR_X+num*mMLen+5, SCR_Y+5, SCR_X+(num+1)*mMLen-5, SCR_Y+25) ;
floodfill(SCR_X+num*mMLen+8, SCR_Y+8, getcolor()) ;
setcolor(TEXT_COLOR+Mode*4) ;
outtextxy(SCR_X+num*mMLen+8, SCR_Y+10, mainMune[num]) ;
}
int drawSubView(int num)
{
int i ;
free(muneBKView) ;
muneBKView = malloc(imagesize(SCR_X+num*mMLen+5
, SCR_Y+25
, SCR_X+(num+1)*mMLen-5
, SCR_Y+25+subNum[num]*SUB_MUNE_H)
) ;
getimage(SCR_X+num*mMLen+5, SCR_Y+25
, SCR_X+(num+1)*mMLen-5, SCR_Y+25+subNum[num]*SUB_MUNE_H
, muneBKView) ;
setcolor(2) ;
rectangle(SCR_X+num*mMLen+5, SCR_Y+25
, SCR_X+(num+1)*mMLen-5, SCR_Y+25+subNum[num]*SUB_MUNE_H) ;
setfillstyle(1, 13) ;
floodfill(SCR_X+num*mMLen+6, SCR_Y+26,getcolor()) ;
for(i = 0;i < subNum[num];i++)
{
line(SCR_X+num*mMLen+7, SCR_Y+25+(i+1)*SUB_MUNE_H
, SCR_X+(num+1)*mMLen-8, SCR_Y+25+(i+1)*SUB_MUNE_H-1) ;
drawSubMune(num, i, i == 0) ;
}
}
int drawSubMune(int num, int choose, int Mode)
{
setcolor((!Mode)?13:4) ;
rectangle(SCR_X+num*mMLen+7, SCR_Y+25+choose*SUB_MUNE_H
, SCR_X+(num+1)*mMLen-8, SCR_Y+25+(choose+1)*SUB_MUNE_H-2) ;
setfillstyle(1, (!Mode)?getcolor():4) ;
floodfill(SCR_X+num*mMLen+8, SCR_Y+26+choose*SUB_MUNE_H, getcolor()) ;
setcolor(TEXT_COLOR+(!Mode)*4) ;
outtextxy(SCR_X+num*mMLen+10, SCR_Y+28+choose*SUB_MUNE_H, subMune[num][choose]) ;
}
int Exit(int Mode)
{
free(muneBKView) ;
free(subMune) ;
closegraph() ;
switch(Mode)
{
case 0:
exit(0) ;
}
}