本文的代码基于上一篇
回到项目demo_xml, 继续编辑。
BasicForm 类中继续增加下面的函数
// 处理菜单弹选项点击事件
bool OnMenuItemClicked(ui::EventArgs* msg);
该函数用做处理菜单选项的点击。
settings按钮关联的回调函数是OnSettingsBtnClicked。现在,增加后的函数体如下:
bool BasicForm::OnSettingsBtnClicked(ui::EventArgs* msg)
{
// 点击设置按钮,弹出菜单
RECT rect = msg->pSender->GetPos();
ui::CPoint point;
point.x = rect.left - 175;
point.y = rect.top + 10;
ClientToScreen(m_hWnd, &point);
nim_comp::CMenuWnd* pmenu = new(std::nothrow) nim_comp::CMenuWnd(NULL);
if (pmenu)
{
ui::STRINGorID xml(L"settings_menu.xml");
pmenu->Init(xml, _T("xml"), point);
// 菜单选项注册回调
//----------------------------------------------------------------------------------------
auto bind_func = [this](nim_comp::CMenuElementUI* pitem)
{
if (pitem)
pitem->AttachSelect(nbase::Bind(&BasicForm::OnMenuItemClicked, this, std::placeholders::_1));
};
nim_comp::CMenuElementUI* pmenu_item1 = (nim_comp::CMenuElementUI*)pmenu->FindControl(L"menu_item1");
nim_comp::CMenuElementUI* pmenu_item2 = (nim_comp::CMenuElementUI*)pmenu->FindControl(L"menu_item2");
nim_comp::CMenuElementUI* pmenu_item3 = (nim_comp::CMenuElementUI*)pmenu->FindControl(L"menu_item3");
bind_func(pmenu_item1);
bind_func(pmenu_item2);
bind_func(pmenu_item3);
}
return false;
}
其中,相对上一篇, 仅增加了下面的代码:
// 菜单选项注册回调
//----------------------------------------------------------------------------------------
auto bind_func = [this](nim_comp::CMenuElementUI* pitem)
{
if (pitem)
pitem->AttachSelect(nbase::Bind(&BasicForm::OnMenuItemClicked, this, std::placeholders::_1));
};
nim_comp::CMenuElementUI* pmenu_item1 = (nim_comp::CMenuElementUI*)pmenu->FindControl(L"menu_item1");
nim_comp::CMenuElementUI* pmenu_item2 = (nim_comp::CMenuElementUI*)pmenu->FindControl(L"menu_item2");
nim_comp::CMenuElementUI* pmenu_item3 = (nim_comp::CMenuElementUI*)pmenu->FindControl(L"menu_item3");
bind_func(pmenu_item1);
bind_func(pmenu_item2);
bind_func(pmenu_item3);
我当时这里卡了,不知道怎么给菜单的选项关联回调函数。后来观察源码demo后发现,需要为每一个菜单选项关联回调函数。就像上面的代码一样,找到每一个菜单选项,进行关联。我这里的代码,三个菜单选项都关联的是同一个回调函数。
这个函数是处理点击菜单选项的回调函数。 其函数体如下:
bool BasicForm::OnMenuItemClicked(ui::EventArgs* msg)
{
if (msg)
{
std::wstring str = msg->pSender->GetName() + std::wstring(L"\n\n");
LPCWSTR result = str.c_str();
OutputDebugString(result);
}
return false;
}
当点击弹出的菜单选项时,将执行这个函数。这个函数将输出到VS的输出窗口中。
手机扫一扫
移动阅读更方便
你可能感兴趣的文章