找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 3639|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

分享12864多級(jí)菜單實(shí)現(xiàn),可方便實(shí)現(xiàn)無(wú)限級(jí)菜單

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:429239 發(fā)表于 2018-11-19 15:21 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
首先定義一個(gè)菜單結(jié)構(gòu)
typedef struct menu//定義一個(gè)菜單
{
u8 range_from,range_to; //當(dāng)前顯示的項(xiàng)開(kāi)始及結(jié)束序號(hào)
u8 itemCount;//項(xiàng)目總數(shù)
u8 selected;//當(dāng)前選擇項(xiàng)
u8 *menuItems[17];//菜單項(xiàng)目
struct menu **subMenus;//子菜單
struct menu *parent;//上級(jí)菜單 ,如果是頂級(jí)則為null
void (**func)();//選擇相應(yīng)項(xiàng)按確定鍵后執(zhí)行的函數(shù)
}Menu;
Menu MainMenu = { //定義主菜單
0,3,4,0,//默認(rèn)顯示0-3項(xiàng),總共4項(xiàng),當(dāng)前選擇第0項(xiàng)
  {
  "設(shè)置1          \x10",
  "設(shè)置2          \x10",
  "輸入指令發(fā)送    ",
  "查詢           \x10"
  }
};
Menu searchMenu = {//查詢菜單
0,3,6,0,
{
  "記帳記錄明細(xì)    ",
  "未采集記錄數(shù)    ",
  "設(shè)備機(jī)號(hào)        ",
  "本機(jī)IP地址      ",
  "記錄空間大小    ",
  "軟件版本        "
}
};
Menu *currentMenu;//當(dāng)前的菜單

用于顯示菜單項(xiàng)
void display(u8 line) //顯示菜單項(xiàng)并設(shè)置選中的項(xiàng)反白
{
int i;
line = 3-(currentMenu->range_to-line);
Lcd_Fill(0x00);
for(i = 0;i<4;i++)
{
  Lcd_Disp(i+1,0,currentMenu->menuItems[i+currentMenu->range_from]);
}
highlight(0,line,8,255);//反白顯示指定行
}
void func(void)
{

printf("hello\r\n");
}
初始化菜單:
void initMenu()
{
MainMenu.subMenus = malloc(sizeof(&MainMenu)*4);
MainMenu.subMenus[0] = NULL;//第1到3項(xiàng)沒(méi)有子菜單置null,選擇后程序會(huì)調(diào)用func中相應(yīng)項(xiàng)中的函數(shù)
MainMenu.subMenus[1] = NULL;
MainMenu.subMenus[2] = NULL;
MainMenu.subMenus[3] = &searchMenu;//第四項(xiàng)查詢有子菜單
MainMenu.func = malloc(sizeof(&func)*4);
MainMenu.func[0] = NULL;
MainMenu.func[1] = NULL;
MainMenu.func[2] = &func;//當(dāng)選擇了并按了確定,會(huì)執(zhí)行func函數(shù)
MainMenu.func[3] = NULL;
MainMenu.parent = NULL;//表示是頂級(jí)菜單

searchMenu.subMenus = malloc(sizeof(&searchMenu)*6);
searchMenu.subMenus[0] = searchMenu.subMenus[1] = searchMenu.subMenus[2] = searchMenu.subMenus[3] = searchMenu.subMenus[4] = searchMenu.subMenus[5] = NULL;
searchMenu.func = malloc(sizeof(&printf)*6);
searchMenu.func[0] = searchMenu.func[2] = searchMenu.func[2] = searchMenu.func[3] = searchMenu.func[4] = searchMenu.func[5] = NULL;
searchMenu.parent = &MainMenu;//上一級(jí)菜單是MainMenu.進(jìn)入查詢子菜單后按返回鍵,將會(huì)顯示這個(gè)菜單 項(xiàng)

currentMenu = &MainMenu;

}

main

int main(void)
{
  
  delay_init();
  uart_init(9600);
  Lcd_Init();
  KEY_Init();
  initMenu();
  display(currentMenu->selected);
  while(1)
{
  key = KEY_Scan();
  if(key == 0)
  {
   delay_ms(10);
   continue;
  }
  printf("key %d is press\r\n",key);
  switch(key)
  {
   case 12://向上
    if(currentMenu->selected == 0)//到了第一項(xiàng)
     break;
    else
    {
     currentMenu->selected--;
     if(currentMenu->selected < currentMenu->range_from)//更新顯示的區(qū)域
     {
      currentMenu->range_from = currentMenu->selected;
      currentMenu->range_to = currentMenu->range_from+3;
     }
     display(currentMenu->selected);
     break;
    }
   case 8://向下
    if(currentMenu->selected == currentMenu->itemCount-1)
     break;
    else
    {
     currentMenu->selected++;
     if(currentMenu->selected>currentMenu->range_to)
     {
      currentMenu->range_to = currentMenu->selected;
      currentMenu->range_from = currentMenu->range_to-3;
     }
     display(currentMenu->selected);
     break;
    }
   case 4://Enter鍵
   {
    if(currentMenu->subMenus[currentMenu->selected] !=NULL)
    {
     currentMenu = currentMenu->subMenus[currentMenu->selected];
     display(0);
    }
    else
    {
     if(currentMenu->func[currentMenu->selected] != NULL)
     {
      currentMenu->func[currentMenu->selected]();//執(zhí)行相應(yīng)的函數(shù)
      display(currentMenu->selected);//返回后恢復(fù)原來(lái)的菜單狀態(tài)
     }
    }
    break;
   }
   case 3://返回鍵
   {
    if(currentMenu->parent!=NULL)//父菜單不為空,將顯示父菜單
    {
     currentMenu = currentMenu->parent;
     display(currentMenu->selected);
    }
    break;
   }
   default:
     break;
  }
  delay_ms(10);
}
}

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:1 發(fā)表于 2018-11-19 18:27 | 只看該作者
補(bǔ)全工程包 即可獲得100+黑幣
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表