gpt4 book ai didi

c++ - 右键单击时从 MenuBar 弹出菜单

转载 作者:行者123 更新时间:2023-11-28 05:09:54 24 4
gpt4 key购买 nike

这是给我带来问题的一段代码:

void CMainFrame::DisplayActionsPopupMenu()
{
// get "Actions" menu
wxMenuBar* pMenuBar = GetMenuBar();
ASSERT(pMenuBar != NULL);
int nIndex = pMenuBar->FindMenu("Actions");
ASSERT(nIndex != wxNOT_FOUND);
wxMenu *pMenuActions = pMenuBar->GetMenu(nIndex);
ASSERT(pMenuActions != NULL);
// display a popup menu for actions
PopupMenu(pMenuActions);
}

我在这里尝试做的是在右键单击时显示一个弹出菜单,我希望它与我项目的菜单栏中的第二个菜单相同。

当我用 wxWidgets v2.8 编译时它起作用了

现在我尝试使用 v3.0,这是错误:

../src/common/menucmn.cpp(715): assert "!IsAttached()" failed in SetInvokingWindow(): menus attached to menu bar can't have invoking window

我应该怎么做才能解决这个问题?

最佳答案

我认为比包含分离和附加菜单的现有答案更强大的解决方案是只创建一个新菜单,例如像这样:

std::unique_ptr<wxMenu> CreateActionsMenu() { ... }

// In your frame ctor or wherever you initialize your menu bar.
MyFrame::MyFrame() {
wxMenuBar* const mb = new wxMenuBar;
mb->Append(CreateActionsMenu().release(), "&Actions");
SetMenuBar(mb);
}

// In your event handler function showing the popup menu.
void MyFrame::OnShowPopup(wxCommandEvent&) {
auto menu = CreateActionsMenu();
PopupMenu(menu.get());
}

创建菜单的速度相对较快,在显示它之前创建菜单应该没有问题(当然,如果它真的很大或构建起来很昂贵,您也可以将其缓存以备后用)。

关于c++ - 右键单击时从 MenuBar 弹出菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43735244/

24 4 0
文章推荐: java - 动态 HTML 表单生成
文章推荐: javascript - Bootstrap 4 : when navbar is collapsed li item is displayed horizontally next to navbar-brand
文章推荐: html - 谁能告诉我为什么我的
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com