为什么菜单被叫做菜单,我想起原因已经无从查考了,这个问题上我再掉故纸堆,罗列一堆各家之言,怕又是要挨骂了。不管你承认与否,菜单的概念已经随着计算机的普及渗透到了我们生活的各个方面。很多东西都可以被称作菜单,很多东西从实质上都拥有菜单的结构。网页中有菜单,网页本身甚至就是一个菜单选项,等等诸如此类。那么菜单拥有怎样的一种结构,我们如何去实现这一结构呢?
要说清楚这个问题,我想从两个方面来说:第一,菜单是由许多具有一定功能的单位按照一定分类组织在一起的图;第二,如果听不懂第一条,就假象菜单是互联网,一个网页就是一个子菜单,子菜单中很多超链接又连接到其他网页,如此往复,他们之间是通过指针,或者说是通过超链接来沟通的。也就是说,我们已经搞清楚了一个菜单的基本结构单元组成就是一个多链表中的一个结构体单元,他往往被简化成树,或者说在执行退回上一级菜单这个功能的时候,被看作是一个树的结构,但是,它实际是图。菜单之间的任何菜单项之间都有可能产生联系。但是记住,菜单一般是多入一出的结构。
说的越来越抽象了哈。其实看不懂前面的文字不要紧,我们看下面的代码就没有那么神秘了。
/***********************
* 结构体宏定义 *
***********************/
struct MenuItem
{
char MenuCount;
char *DisplayString;
void (*Subs)();
struct MenuItem *ChildrenMenus;
struct MenuItem *ParentMenus;
}NULL_MENU;
void NULL_FUNCTION(void){}
我们看到,菜单的最小单元MenuItem的组成其实非常简单:
菜单项所在层的菜单项数目(度);
菜单项显示出来的字符串;
菜单需要执行的功能的函数指针(可以为空);
孩子指针(表明选择该菜单项后,跳转到哪个子菜单去);
父指针(表明选择ESC后,跳转到哪个菜单去);
看一个菜单实例:
(源代码)
struct MenuItem MainMenu[3];
struct MenuItem TimeMenu[4];
struct MenuItem VoiceMenu[5];
struct MenuItem RobotMenu[5];
struct MenuItem FlashMenu[5];
/***********************
* 函 数 声 明 区 *
***********************/
void MainMenuInit(void);
void TimeMenuInit(void);
void VoiceMenuInit(void);
void RobotMenuInit(void);
void FlashMenuInit(void);
/**************************************************************
* 函数说明:Flash处理目录初始化函数 *
**************************************************************/
void FlashMenuInit(void)
{
FlashMenu[0].MenuCount = 5;
FlashMenu[0].DisplayString = " Flash Record ";
FlashMenu[0].Subs = FlashRecord;
FlashMenu[0].ChildrenMenus = &Null;
FlashMenu[0].ParentMenus = MainMenu;
FlashMenu[1].MenuCount = 5;
FlashMenu[1].DisplayString = " Play ";
FlashMenu[1].Subs = FlashPlay;
FlashMenu[1].ChildrenMenus = &Null;
FlashMenu[1].ParentMenus = MainMenu;
FlashMenu[2].MenuCount = 5;
FlashMenu[2].DisplayString = " Pause ";
FlashMenu[2].Subs = FlashPause;
FlashMenu[2].ChildrenMenus = &Null;
FlashMenu[2].ParentMenus = MainMenu;
FlashMenu[3].MenuCount = 5;
FlashMenu[3].DisplayString = " Flash Delete ";
FlashMenu[3].Subs = FlashDelete;
FlashMenu[3].ChildrenMenus = &Null;
FlashMenu[3].ParentMenus = MainMenu;
FlashMenu[4].MenuCount = 5;
FlashMenu[4].DisplayString = " Back ";
FlashMenu[4].Subs = NullSubs;
FlashMenu[4].ChildrenMenus = MainMenu;
FlashMenu[4].ParentMenus = MainMenu;
}
/**************************************************************
* 函数说明:机器人控制目录初始化函数 *
**************************************************************/
void RobotMenuInit(void)
{
RobotMenu[0].MenuCount = 5;
RobotMenu[0].DisplayString = " Turn Left ";
RobotMenu[0].Subs = RobotTurnLeft;
RobotMenu[0].ChildrenMenus = &Null;
RobotMenu[0].ParentMenus = MainMenu;
RobotMenu[1].MenuCount = 5;
RobotMenu[1].DisplayString = " Turn Right ";
RobotMenu[1].Subs = RobotTurnRight;
RobotMenu[1].ChildrenMenus = &Null;
RobotMenu[1].ParentMenus = MainMenu;
RobotMenu[2].MenuCount = 5;
RobotMenu[2].DisplayString = " Go Ahead ";
RobotMenu[2].Subs = RobotGoAhead;
RobotMenu[2].ChildrenMenus = &Null;
RobotMenu[2].ParentMenus = MainMenu;
RobotMenu[3].MenuCount = 5;
RobotMenu[3].DisplayString = " Go Back ";
RobotMenu[3].Subs = RobotGoBack;
RobotMenu[3].ChildrenMenus = &Null;
RobotMenu[3].ParentMenus = MainMenu;
RobotMenu[4].MenuCount = 5;
RobotMenu[4].DisplayString = " Back ";
RobotMenu[4].Subs = NullSubs;
RobotMenu[4].ChildrenMenus = MainMenu;
RobotMenu[4].ParentMenus = MainMenu;
}
/**************************************************************
* 函数说明:声音处理目录初始化函数 *
**************************************************************/
void VoiceMenuInit(void)
{
VoiceMenu[0].MenuCount = 5;
VoiceMenu[0].DisplayString = " Voice Record ";
VoiceMenu[0].Subs = VoiceRecord;
VoiceMenu[0].ChildrenMenus = &Null;
VoiceMenu[0].ParentMenus = MainMenu;
VoiceMenu[1].MenuCount = 5;
VoiceMenu[1].DisplayString = " Play ";
VoiceMenu[1].Subs = Play;
VoiceMenu[1].ChildrenMenus = &Null;
VoiceMenu[1].ParentMenus = MainMenu;
VoiceMenu[2].MenuCount = 5;
VoiceMenu[2].DisplayString = " Pause ";
VoiceMenu[2].Subs = Pause;
VoiceMenu[2].ChildrenMenus = &Null;
VoiceMenu[2].ParentMenus = MainMenu;
VoiceMenu[3].MenuCount = 5;
VoiceMenu[3].DisplayString = " Voice Delete ";
VoiceMenu[3].Subs = VoiceDelete;
VoiceMenu[3].ChildrenMenus = &Null;
VoiceMenu[3].ParentMenus = MainMenu;
VoiceMenu[4].MenuCount = 5;
VoiceMenu[4].DisplayString = " Back ";
VoiceMenu[4].Subs = NullSubs;
VoiceMenu[4].ChildrenMenus = MainMenu;
VoiceMenu[4].ParentMenus = MainMenu;
}
/**************************************************************
* 函数说明:时间设定子目录初始化 *
**************************************************************/
void TimeMenuInit(void)
{
TimeMenu[0].MenuCount = 4;
TimeMenu[0].DisplayString = " Time Set ";
TimeMenu[0].Subs = TimeSet;
TimeMenu[0].ChildrenMenus = &Null;
TimeMenu[0].ParentMenus = MainMenu;
TimeMenu[1].MenuCount = 4;
TimeMenu[1].DisplayString = " Date Set ";
TimeMenu[1].Subs = DateSet;
TimeMenu[1].ChildrenMenus = &Null;
TimeMenu[1].ParentMenus = MainMenu;
TimeMenu[2].MenuCount = 4;
TimeMenu[2].DisplayString = " AlertSet ";
TimeMenu[2].Subs = AlertSet;
TimeMenu[2].ChildrenMenus = &Null;
TimeMenu[2].ParentMenus = MainMenu;
TimeMenu[3].MenuCount = 4;
TimeMenu[3].DisplayString = " Back ";
TimeMenu[3].Subs = NullSubs;
TimeMenu[3].ChildrenMenus = MainMenu;
TimeMenu[3].ParentMenus = MainMenu;
}
/**************************************************************
* 函数说明:根目录初始化 *
**************************************************************/
void MainMenuInit(void)
{
MainMenu[0].MenuCount = 3;
MainMenu[0].DisplayString = " Time Set ";
MainMenu[0].Subs = NullSubs;
MainMenu[0].ChildrenMenus = TimeMenu;
MainMenu[0].ParentMenus = &Null;
MainMenu[1].MenuCount = 3;
MainMenu[1].DisplayString = " Voice Center ";
MainMenu[1].Subs = NullSubs;
MainMenu[1].ChildrenMenus = VoiceMenu;
MainMenu[1].ParentMenus = &Null;
/*
MainMenu[2].MenuCount = 3;
MainMenu[2].DisplayString = " Robot Control ";
MainMenu[2].Subs = NullSubs;
MainMenu[2].ChildrenMenus = RobotMenu;
MainMenu[2].ParentMenus = &Null;
*/
MainMenu[2].MenuCount = 3;
MainMenu[2].DisplayString = " Flash Option ";
MainMenu[2].Subs = NullSubs;
MainMenu[2].ChildrenMenus = FlashMenu;
MainMenu[2].ParentMenus = &Null;
}
struct MenuItem (*MenuPoint) = MainMenu;
short DisplayStart = 0;
short UserChoose = 0;
short DisplayPoint = 0;
short MaxItems;
/*****************************
* Struct MenuItem: *
* short MenuCount; *
* char *DisplayString; *
* void (*Subs)(); *
* MenuItem *ChildrenMenus;*
* MenuItem *ParentMenus; *
*****************************/
/***********************
* 函 数 声 明 区 *
***********************/
extern void ClearWatchDog();
void MenuInitialation(void);
void SystemInitialation(void);
void ShowMenu(void);
short GetKeyNum(void);
/**************************************************************
* 函数说明:系统初始化函数 *
**************************************************************/
void SystemInitialation(void)
{
Init_sys();
Enable_LCD(); //初始化字库 函数定义在Splc501sys.asm
MenuInitialation(); //初始化菜单
GRAPH //图形初始化
}
/**************************************************************
* 函数说明:目录初始化函数 *
**************************************************************/
void MenuInitialation(void)
{
MainMenuInit();
TimeMenuInit();
VoiceMenuInit();
RobotMenuInit();
FlashMenuInit();
}
/**************************************************************
* 函数说明:目录显示函数 *
**************************************************************/
void ShowMenu(void)
{
short n = 0;
MaxItems = MenuPoint[0].MenuCount;
DisplayPoint = DisplayStart;
if (MaxItems >= 4)
{
for (n = 0;n<4;n++)
{
LOCATE(n+1,1);
PRINT(MenuPoint[DisplayPoint].DisplayString);
if ((DisplayPoint) == UserChoose)
{
BOX(1,n*16+1,126,(n+1)*16-2,1,1);
}
DisplayPoint +=1;
if ((DisplayPoint) == (MaxItems))
{
DisplayPoint = 0;
}
}
}
else
{
for (n = 0;n
{
LOCATE(n+1,1);
PRINT(MenuPoint[DisplayPoint].DisplayString);
if ((DisplayPoint) == UserChoose)
{
BOX(1,n*16+1,126,(n+1)*16-2,1,1);
}
DisplayPoint +=1;
if ((DisplayPoint) == (MaxItems))
{
DisplayPoint = 0;
}
}
}
//BOX(0,0,127,63,2,2);
}
/**************************************************************
* 函数说明:获得键值函数 *
**************************************************************/
short GetKeyNum(void)
{
short TempKeyNum = 0;
TempKeyNum = F_Key_Scan(); //获取按键值
*P_IOA_Dir = 0x01ff;
*P_IOA_Attrib = 0x01ff;
*P_IOA_Data = 0x01ff;
return TempKeyNum;
}
/**************************************************************
* 函数说明:主函数 *
**************************************************************/
int main()
{
short KeyNum = 0xff;
SystemInitialation(); //系统初始化
ShowMenu();
while(1)
{
ClearWatchDog(); //喂狗
KeyNum = GetKeyNum(); //获取按键值
/*******************目录操作*********************/
/***************************************
* [按键说明] *
* ----------------------------------- *
* [K1] UP(向上) *
* [K5] Down(向下) *
* [K2] Esc(后退) *
* [K6] Enter(确定) *
* [K3] 返回根目录 *
***************************************/
if (KeyNum != 0xff)
{
ShowMenu();
switch(KeyNum)
{
case UP:
UserChoose --;
if (UserChoose < 0)
{
UserChoose = MaxItems-1;
}
break;
case Esc:
if (MenuPoint[0].ParentMenus != &Null)
{
MenuPoint = MenuPoint[0].ParentMenus;
UserChoose = 0;
DisplayStart = 0;
}
break;
case Down:
UserChoose ++;
if (UserChoose == MaxItems)
{
UserChoose = 0;
}
break;
case Enter:
if (MenuPoint[UserChoose].Subs != NullSubs)
{
(*MenuPoint[UserChoose].Subs)();
}
else if (MenuPoint[UserChoose].ChildrenMenus != &Null)
{
MenuPoint = MenuPoint[UserChoose].ChildrenMenus;
UserChoose = 0;
DisplayStart = 0;
}
break;
case Reset:
MenuPoint = MainMenu;
UserChoose = 0;
DisplayStart = 0;
break;
}
if ((UserChoose < DisplayStart) || (UserChoose > (DisplayStart+3)))
{
DisplayStart = UserChoose;
}
CLS
ShowMenu();
}
/*******************目录操作*********************/
}
}
过多的废话我就不多说了,大家看了就清楚了哈。
顺便提示下,其实用在串口中的指令系统也是一种菜单,用的好的话,可以用菜单的结构来描述很复杂的指令系统哦。
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』