* 要定义一个菜单,需要做以下几步工作 *
* 1、定义一个结构体数组 *
* 2、如果这个菜单是子菜单,那么把上级菜单的 *
* *ChildrenMenus指针指向该数组的首地址, *
* 并且设置ParentMenus为上层目录的地址 *
* 3、如果这个菜单是跟菜单,那么直接将MenuPoint *
* 指针指向该数组的首地址 *
* 4、通过写一个函数为该数组的每一个元素初始化 *
* 5、如果菜单项是最终选项,那么将*ChildrenMenus*
* 置为NULL,将函数指针*Subs指向实现功能的 *
* 函数。 *
菜单结构
MAIN→FILE EDIT VIEW HELP
FILE →OPEN SAVE SAVEAS PRINT EXIT
EDIT→UNDO REDO COPY PASTE
VIEW→FONT COLOR
FONT→SONGTI,HEITI,YOUYUAN
HELP→INDEX ONLINE ABOUT
功能键
0-同级向后移
1-同级向前移
+下一级或执行某功能
- 上一级
其他注释,漫游的方式是基址加偏移量,
manyou是基址,即为某一级菜单
menuid记录了偏移量,即在该菜单中的位置
走到exit处才能退出程序.
//源程序
#include <stdio.h>
#define Null 0
char over=0; //结束标志
//====================================
//结构体类型定义
struct MenuItem
{
char MenuCount; //结构体数组的元素个数
char *DisplayString; //当前LCD显示的信息
void (*Subs)(); //执行的函数的指针.
struct MenuItem *Childrenms; //指向子节点的指针
struct MenuItem *Parentms; //指向父节点的指针
};
//====================================
//函数声明
void Nop(void);
void DoSomething(struct MenuItem *manyou);
void GameOver(void);
void Locate(struct MenuItem *manyou);
void Run(struct MenuItem *manyou);
//====================================
//结构体声明
struct MenuItem m0_main[4];
struct MenuItem m1_file[5];
struct MenuItem m1_edit[4];
struct MenuItem m1_view[2];
struct MenuItem m2_font[3];
struct MenuItem m1_help[3];
//====================================
//结构体实现
struct MenuItem m0_main[4]=
{
{4,"file",Nop,m1_file,Null},
{4,"edit",Nop,m1_edit,Null},
{4,"view",Nop,m1_view,Null},
{4,"help",Nop,m1_help,Null},
};
struct MenuItem m1_file[5]=
{
{5,"open",DoSomething,Null,m0_main},
{5,"save",DoSomething,Null,m0_main},
{5,"save as",DoSomething,Null,m0_main},
{5,"print",DoSomething,Null,m0_main},
{5,"exit",GameOver,Null,m0_main},
};
struct MenuItem m1_edit[4]=
{
{4,"undo",DoSomething,Null,m0_main},
{4,"redo",DoSomething,Null,m0_main},
{4,"copy",DoSomething,Null,m0_main},
{4,"paste",DoSomething,Null,m0_main},
};
struct MenuItem m1_view[2]=
{
{2,"font",Nop,m2_font,m0_main},
{2,"color",DoSomething,Null,m0_main},
};
struct MenuItem m2_font[3]=
{
{3,"songti",DoSomething,Null,m1_view},
{3,"heiti",DoSomething,Null,m1_view},
{3,"youyuan",DoSomething,Null,m1_view},
};
struct MenuItem m1_help[3]=
{
{3,"index",DoSomething,Null,m0_main},
{3,"online",DoSomething,Null,m0_main},
{3,"about",DoSomething,Null,m0_main},
};
//====================================
//函数实现
void Locate(struct MenuItem *manyou)
{
printf("Now we at \"%s\"
",manyou->DisplayString);
}
void Run(struct MenuItem *manyou)
{
(*(manyou->Subs))(manyou);
}
void Nop(void)
{}
void DoSomething(struct MenuItem *manyou)
{
printf("we have done \"%s\"
",manyou->DisplayString);
}
void GameOver(void)
{
over=1;
printf("Now Game is Over
");
getchar();
}
//====================================
//主函数
int main(int argc, char *argv[])
{
char charin; //输入的字符
char menuid[3]={0,0,0}; //某一级的菜单的偏移量的记录数组
char i=0; //上面数组的下标值
struct MenuItem *manyou; //漫游,在菜单中漫游
manyou=&m0_main[0]; //开始的位置
Locate(manyou); //输出
while (!over) //如果还没完
{
charin=getchar(); //读取键盘字符
switch (charin) //根据字符跳转
{
case '0': //到同级菜单的后一项
{
menuid++;
if (menuid>manyou->MenuCount-1) menuid=0;//可以看到MenuCount项是必要的
Locate(manyou+menuid);
break;
}
case '1': //到同级菜单的前一项
{
if (menuid==0) menuid=manyou->MenuCount-1;//可以看到MenuCount项是必要的
else menuid--;
Locate(manyou+menuid);
break;
}
case '+': //到下一级菜单,无下级时执行某功能
{
if ((manyou+menuid)->Childrenms !=Null)
{
manyou=(manyou+menuid)->Childrenms;
i++;
menuid=0;
Locate(manyou+menuid);
}
else
{
Run(manyou+menuid);
}
break;
}
case '-': //到上一级菜单,无上级时给出些提示
{
if ((manyou+menuid)->Parentms !=Null)
{
manyou=(manyou+menuid)->Parentms;
i--;
Locate(manyou+menuid);
}
else
{
printf("You are at the top of menu
");
}
break;
}
default: break;
}
}
return 0;
}
文章评论(0条评论)
登录后参与讨论