定义菜单结构体变量
struct MenuItem
{
short MenuCount;//当前菜单项目总数
u8 *DisplayString;//当前项目要显示的字符
void(*Subs)();//选择某一菜单后执行的功能函数
struct MenuItem *ChildrenMenus;//当前项目的子菜单
struct MenuItem *ParentMenus;//当前项目的父菜单
}
void display(struct MenuItem * MenuPoint,short selectItem)//显示函数
{
int i;
int j;
u16 x=30;
u16 y=30;
for ( i = 0; i < (selectItem-1); i++)
{
LCD_ShowString(x,y,200,16,16,MenuPoint[i].DisplayString);
y+=30;
}
我自己定义的菜单结构体变量:
struct MenuItem MainMenu[3] = {
{ 3,"set1",NULL,NULL,NULL },
{ 3,"set2",NULL,NULL,NULL },
{ 3,"set3",NULL,NULL,NULL }
};
struct MenuItem Setmenu1[2]={
{ 2,"timeset1",NULL,NULL,MainMenu },
{ 2,"Voiceset1",NULL,NULL,MainMenu }};
struct MenuItem Setmenu2[2] = {
{ 2,"timeset2",NULL,NULL,MainMenu },
{ 2,"Voiceset2",NULL,NULL,MainMenu } };
struct MenuItem Setmenu11[2]={
{ 2,"timeset1.1",NULL,NULL,Setmenu1 },
{ 2,"Voiceset1.1",NULL,NULL,Setmenu1 }};
struct MenuItem* MenuPoint = MainMenu
short selectItem;
通过修改MenuPoint指向不同的菜单数组,即可显示不同的菜单
下面附上源代码(我采用了红外遥控)
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "lcd.h"
//#include "adc.h"
//#include "timer.h"
//#include "pwm.h"
//#include "key.h"
//#include "pid.h"
#include "remote.h"
struct MenuItem
{
short MenuCount;
u8 *DisplayString;
void(*Subs)();
struct MenuItem *ChildrenMenus;
struct MenuItem *ParentMenus;
};
struct MenuItem MainMenu[3] = {
{ 3,"set1",NULL,NULL,NULL },
{ 3,"set2",NULL,NULL,NULL },
{ 3,"set3",NULL,NULL,NULL }
};
struct MenuItem Setmenu1[2]={
{ 2,"timeset1",NULL,NULL,MainMenu },
{ 2,"Voiceset1",NULL,NULL,MainMenu }};
struct MenuItem Setmenu2[2] = {
{ 2,"timeset2",NULL,NULL,MainMenu },
{ 2,"Voiceset2",NULL,NULL,MainMenu } };
struct MenuItem Setmenu11[2]={
{ 2,"timeset1.1",NULL,NULL,Setmenu1 },
{ 2,"Voiceset1.1",NULL,NULL,Setmenu1 }};
struct MenuItem* MenuPoint = MainMenu;
short selectItem;
void display(struct MenuItem * MenuPoint,short selectItem)
{
int i;
int j;
u16 x=30;
u16 y=30;
for ( i = 0; i < (selectItem-1); i++)
{
LCD_ShowString(x,y,200,16,16,MenuPoint[i].DisplayString);
y+=30;
}
POINT_COLOR=BLACK;
LCD_DrawRectangle(x-5,y-10,x+60,y+20);
LCD_ShowString(x,y,200,16,16,MenuPoint[selectItem-1].DisplayString);
y+=30;
for ( j= selectItem; j < MenuPoint->MenuCount; j++)
{
LCD_ShowString(x,y,200,16,16,MenuPoint[j].DisplayString);
y+=30;
}
}
int main(void)
{
u8 key=0;
delay_init();
uart_init(9600);
LCD_Init();
Remote_Init();
POINT_COLOR=RED;//
MainMenu[0].ChildrenMenus = Setmenu1;
MainMenu[1].ChildrenMenus = Setmenu2;
Setmenu1[0].ChildrenMenus=Setmenu11;
selectItem =1;
display(MenuPoint,selectItem);
while(1)
{
key=Remote_Scan();
delay_ms(10);
switch(key)
{
case 98:{//上
LCD_Clear(WHITE);
if (selectItem == 1) selectItem = MenuPoint->MenuCount;
else selectItem--;
display(MenuPoint, selectItem);
key=0;
};break;
case 168:{/下
LCD_Clear(WHITE);
if (selectItem == MenuPoint->MenuCount) selectItem = 1;
else selectItem++;
display(MenuPoint, selectItem);
key=0;
};break;
case 194:{//右 确认进入此项目
if (MenuPoint[selectItem - 1].ChildrenMenus != NULL)
{
LCD_Clear(WHITE);
MenuPoint = MenuPoint[selectItem - 1].ChildrenMenus;
selectItem = 1;
display(MenuPoint, selectItem);
}
key=0;
};break;
case 34:{//左 返回
if (MenuPoint[selectItem - 1].ParentMenus != NULL)
{
LCD_Clear(WHITE);
MenuPoint = MenuPoint[selectItem - 1].ParentMenus;
selectItem = 1;
display(MenuPoint, selectItem);
}
key=0;
};break;
}
delay_ms(10);
}
}