菜单需要两个类:
1.CMenu: 菜单,相当于菜单的一个容器,框!
2.CMenuItem:菜单项,也是我们滚动焦点来选中的菜单项,还可以相应事件
使用方法:
继承 MMenuObserver 接口
初始化:
iMenu = new (ELeave) CMenu(*this);
设置位置:
if (iMenu)
iMenu->SetRect(TRect(aRect.iTl.iX, aRect.Height() - iMenu->GetHeight() + aRect.iTl.iY - iSoftKeyBar->GetHeight(), aRect.iTl.iX
+ iMenu->GetWidth() + 20, aRect.iBr.iY - iSoftKeyBar->GetHeight()));
绘制:
if (iMenu && iMenu->IsVisible())
iMenu->Draw(gc);
响应按键:
if (iMenu && !iMenu->IsVisible())
{
iMenu->SetVisible(ETrue);
return EKeyWasConsumed;
}
else if (iMenu->IsVisible())
{
TKeyEvent event;
event.iCode = EKeyOK;
return OfferKeyEventL(event, aType);
}
添加菜单项:
iMenu->RemoveAllMenuItem();
CMenuItem* itemOpen = new (ELeave) CMenuItem(_L("Open"), 0);
iMenu->AddMenuItem(itemOpen);
CMenuItem* itemSetting = new (ELeave) CMenuItem(_L("Setting"), 1);
iMenu->AddMenuItem(itemSetting);
CMenuItem* itemHelp = new (ELeave) CMenuItem(_L("Help"), 2);
iMenu->AddMenuItem(itemHelp);
CMenuItem* itemAbout = new (ELeave) CMenuItem(_L("About"), 3);
iMenu->AddMenuItem(itemAbout);
CMenuItem* itemExit = new (ELeave) CMenuItem(_L("Exit"), 4);
iMenu->AddMenuItem(itemExit);
iMenu->SetVisible(EFalse);
析构:
delete iMenu;