主题:[原创]菜单演示程序-《C高级实用程序设计》 王士元 p304 - p320
/*
菜单演示程序
《C高级实用程序设计》 王士元编著 清华大学出版社 p304 - p320
TC3.0 下编译通过.对源代码略有修改。
meteor135
smith_135@163.com
2005-5-21 1:11
*/
#include<graphics.h>
#include<alloc.h>
#include<process.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<dos.h>
#include<bios.h>
#include<conio.h>
#include<ctype.h>
#define INSERT 0x5200
#define ESC 0x001b
#define TAB 0x0f09
#define RETURN 0x000d
#define RIGHT 0x4d00
#define LEFT 0x4b00
#define UP 0x4800
#define DOWN 0x5000
#define BS 0x0e08
#define HOME 0x4700
#define END 0x4f00
#define PGUP 0x4900
#define PGDN 0x5100
#define DEL 0x5300
#define F1 0x3b00
#define F2 0x3c00
#define F3 0x3d00
#define F4 0x3e00
#define F5 0x3f00
#define F6 0x4000
#define F7 0x4100
#define F8 0x4200
#define F9 0x4300
#define F10 0x4400
#define MenuInGround 1
#define MenuActiveGround 2
#define MenuActiveItem 3
#define MenuInItem 4
#define MenuEdge 5
#define WindowEdge 6
#define WindowGround 7
#define PopWindow 8
#define WindowMinX 1
#define WindowMinY 25
#define WindowMaxX 638
#define WindowMaxY 478
void getitemcount(char **,int *,int *,int *);
void InitialGraphics(void);
void LoadMainMenu(void);
void LoadSubMenu(char **name,int ord);
int ManageSubMenu(void);
void ManageMainMenu(void);
void DisplayMainMenu(void);
int DisplaySubMenu(int ord);
void FuncProc(int ID);
void GoodBye(char *pcInf1,char *pcInf2);
void In_ActiveMainMenuItem(int select,char In_Active);
void In_ActiveSubMenuItem(int ord,int select,char In_Active);
int GetKey(void);
void MessageBox(char *Message);
void About(void);
void MoveText(void);
void Draw(void);
void GoodBye(char *pcInf1,char *pcInf2);
void ExitMenu(int ord);
int DisplaySubMenu(int ord);
void Help_page_show(void){return;};
unsigned char AllColors[20]={0,CYAN,LIGHTRED,YELLOW,BLACK,WHITE,LIGHTRED,BLUE,CYAN,9,10,11,12,13,14,15};
#define INUM 10 /* maximum 10 items in each MENU */
typedef struct _menu
{
int coor[4]; /* The menu's area */
int itemcoor[4*INUM]; /* max 10 item,each with x1,y1,x2,y2 */
int itemdispxy[2*INUM]; /* Actually disp item name's X coor */
char select; /* selected item's ord */
char itemnum; /* The number of items in this menu */
char **itemname; /* item's name(point to static data) */
int COMMAND_ID[INUM]; /* Key number */
}MENUTYPE;
MENUTYPE MainMenu,SubMenu[INUM];
static char *MainMenuItem[]={"File","Menu1","Menu2","Menu3","Menu4","Quit",0};
static char *SubMenuItem1[]=
{
"Item11",
"Item12",
"Item13",
"Item14",
"Item15",
"Item16",
"Quit",
0
};
static char *SubMenuItem2[]=
{
"Item21",
"Draw",
"Item23",
"Item24",
"Item25",
"Item26",
"Item27",
"Item28",
0
};
static char *SubMenuItem3[]=
{
"Item31...",
"Item32...",
"Item add......",
"Item33",
0
};
static char *SubMenuItem4[]=
{
"Item41...",
"Item42...",
"Moving Text",
"Item44",
"Item45",
"iteme**",
0
};
static char *SubMenuItem5[]=
{
"Item51...",
"About...",
"Item53",
0
};
static char *SubMenuItem6[]=
{
0
};
int maxx,texth,textw;
void * MenuImageBuf;
void main( )
{
InitialGraphics();
maxx=getmaxx();
texth=textheight("text");
textw=textwidth("t");
LoadMainMenu();
LoadSubMenu(SubMenuItem1,0);
LoadSubMenu(SubMenuItem2,1);
LoadSubMenu(SubMenuItem3,2);
LoadSubMenu(SubMenuItem4,3);
LoadSubMenu(SubMenuItem5,4);
LoadSubMenu(SubMenuItem6,5);
ManageMainMenu();
}
/*checked 2005年5月20日 23:58:03 */
void ManageMainMenu(void)
{
unsigned key;
int ID;
DisplayMainMenu();
for(key=0;;)
{
key=GetKey();
switch(key)
{
case LEFT:
In_ActiveMainMenuItem(MainMenu.select,0);
if(MainMenu.select<1) /*Already leftest item in mainmenu */
MainMenu.select=MainMenu.itemnum-1;
else
MainMenu.select--;
In_ActiveMainMenuItem(MainMenu.select,1);/* checked 2005年5月20日 23:58:03*/
break;
case RIGHT:
In_ActiveMainMenuItem(MainMenu.select,0);
if(MainMenu.select>MainMenu.itemnum-2) /*already rightest */
MainMenu.select=0;
else
MainMenu.select++;
In_ActiveMainMenuItem(MainMenu.select,1);
break;
case DOWN:
case RETURN:
switch(MainMenu.COMMAND_ID[MainMenu.select])
{
case 5: /* Exit to DOS */
if(key==DOWN)
break;
closegraph();
GoodBye("Goodbye from","the MENU DEMO in graphics mode.");
break;
default:
ID=ManageSubMenu();
if(ID>=0)
FuncProc(ID);
break;
}
}
}
}
void FuncProc(int ID)
{
switch(ID)
{
case 6: /* 006 File|Quit */
closegraph();
GoodBye("GoodBye from","the MENU DEMO in graphics mode.");
break;
case 101:
Draw();
break;
case 302:
MoveText();
break;
case 401:
About();
break;
default:
/* Clear window */
setfillstyle(SOLID_FILL,AllColors[WindowGround]);
bar(WindowMinX,WindowMinY,WindowMaxX,WindowMaxY);
/* dynamic show item text 2005年5月21日 1:07:50*/
MessageBox(*(SubMenu[MainMenu.select].itemname + SubMenu[MainMenu.select].select));
/* MessageBox("This is an example!");*/
break;
}
}
菜单演示程序
《C高级实用程序设计》 王士元编著 清华大学出版社 p304 - p320
TC3.0 下编译通过.对源代码略有修改。
meteor135
smith_135@163.com
2005-5-21 1:11
*/
#include<graphics.h>
#include<alloc.h>
#include<process.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<dos.h>
#include<bios.h>
#include<conio.h>
#include<ctype.h>
#define INSERT 0x5200
#define ESC 0x001b
#define TAB 0x0f09
#define RETURN 0x000d
#define RIGHT 0x4d00
#define LEFT 0x4b00
#define UP 0x4800
#define DOWN 0x5000
#define BS 0x0e08
#define HOME 0x4700
#define END 0x4f00
#define PGUP 0x4900
#define PGDN 0x5100
#define DEL 0x5300
#define F1 0x3b00
#define F2 0x3c00
#define F3 0x3d00
#define F4 0x3e00
#define F5 0x3f00
#define F6 0x4000
#define F7 0x4100
#define F8 0x4200
#define F9 0x4300
#define F10 0x4400
#define MenuInGround 1
#define MenuActiveGround 2
#define MenuActiveItem 3
#define MenuInItem 4
#define MenuEdge 5
#define WindowEdge 6
#define WindowGround 7
#define PopWindow 8
#define WindowMinX 1
#define WindowMinY 25
#define WindowMaxX 638
#define WindowMaxY 478
void getitemcount(char **,int *,int *,int *);
void InitialGraphics(void);
void LoadMainMenu(void);
void LoadSubMenu(char **name,int ord);
int ManageSubMenu(void);
void ManageMainMenu(void);
void DisplayMainMenu(void);
int DisplaySubMenu(int ord);
void FuncProc(int ID);
void GoodBye(char *pcInf1,char *pcInf2);
void In_ActiveMainMenuItem(int select,char In_Active);
void In_ActiveSubMenuItem(int ord,int select,char In_Active);
int GetKey(void);
void MessageBox(char *Message);
void About(void);
void MoveText(void);
void Draw(void);
void GoodBye(char *pcInf1,char *pcInf2);
void ExitMenu(int ord);
int DisplaySubMenu(int ord);
void Help_page_show(void){return;};
unsigned char AllColors[20]={0,CYAN,LIGHTRED,YELLOW,BLACK,WHITE,LIGHTRED,BLUE,CYAN,9,10,11,12,13,14,15};
#define INUM 10 /* maximum 10 items in each MENU */
typedef struct _menu
{
int coor[4]; /* The menu's area */
int itemcoor[4*INUM]; /* max 10 item,each with x1,y1,x2,y2 */
int itemdispxy[2*INUM]; /* Actually disp item name's X coor */
char select; /* selected item's ord */
char itemnum; /* The number of items in this menu */
char **itemname; /* item's name(point to static data) */
int COMMAND_ID[INUM]; /* Key number */
}MENUTYPE;
MENUTYPE MainMenu,SubMenu[INUM];
static char *MainMenuItem[]={"File","Menu1","Menu2","Menu3","Menu4","Quit",0};
static char *SubMenuItem1[]=
{
"Item11",
"Item12",
"Item13",
"Item14",
"Item15",
"Item16",
"Quit",
0
};
static char *SubMenuItem2[]=
{
"Item21",
"Draw",
"Item23",
"Item24",
"Item25",
"Item26",
"Item27",
"Item28",
0
};
static char *SubMenuItem3[]=
{
"Item31...",
"Item32...",
"Item add......",
"Item33",
0
};
static char *SubMenuItem4[]=
{
"Item41...",
"Item42...",
"Moving Text",
"Item44",
"Item45",
"iteme**",
0
};
static char *SubMenuItem5[]=
{
"Item51...",
"About...",
"Item53",
0
};
static char *SubMenuItem6[]=
{
0
};
int maxx,texth,textw;
void * MenuImageBuf;
void main( )
{
InitialGraphics();
maxx=getmaxx();
texth=textheight("text");
textw=textwidth("t");
LoadMainMenu();
LoadSubMenu(SubMenuItem1,0);
LoadSubMenu(SubMenuItem2,1);
LoadSubMenu(SubMenuItem3,2);
LoadSubMenu(SubMenuItem4,3);
LoadSubMenu(SubMenuItem5,4);
LoadSubMenu(SubMenuItem6,5);
ManageMainMenu();
}
/*checked 2005年5月20日 23:58:03 */
void ManageMainMenu(void)
{
unsigned key;
int ID;
DisplayMainMenu();
for(key=0;;)
{
key=GetKey();
switch(key)
{
case LEFT:
In_ActiveMainMenuItem(MainMenu.select,0);
if(MainMenu.select<1) /*Already leftest item in mainmenu */
MainMenu.select=MainMenu.itemnum-1;
else
MainMenu.select--;
In_ActiveMainMenuItem(MainMenu.select,1);/* checked 2005年5月20日 23:58:03*/
break;
case RIGHT:
In_ActiveMainMenuItem(MainMenu.select,0);
if(MainMenu.select>MainMenu.itemnum-2) /*already rightest */
MainMenu.select=0;
else
MainMenu.select++;
In_ActiveMainMenuItem(MainMenu.select,1);
break;
case DOWN:
case RETURN:
switch(MainMenu.COMMAND_ID[MainMenu.select])
{
case 5: /* Exit to DOS */
if(key==DOWN)
break;
closegraph();
GoodBye("Goodbye from","the MENU DEMO in graphics mode.");
break;
default:
ID=ManageSubMenu();
if(ID>=0)
FuncProc(ID);
break;
}
}
}
}
void FuncProc(int ID)
{
switch(ID)
{
case 6: /* 006 File|Quit */
closegraph();
GoodBye("GoodBye from","the MENU DEMO in graphics mode.");
break;
case 101:
Draw();
break;
case 302:
MoveText();
break;
case 401:
About();
break;
default:
/* Clear window */
setfillstyle(SOLID_FILL,AllColors[WindowGround]);
bar(WindowMinX,WindowMinY,WindowMaxX,WindowMaxY);
/* dynamic show item text 2005年5月21日 1:07:50*/
MessageBox(*(SubMenu[MainMenu.select].itemname + SubMenu[MainMenu.select].select));
/* MessageBox("This is an example!");*/
break;
}
}