简体   繁体   English

如何通过wxWidgets中的`wxWindow :: PopupMenu`以编程方式关闭用作弹出菜单的wxMenu?

[英]How to programmatically close wxMenu used as popup menu via `wxWindow::PopupMenu` in wxWidgets?

I have a set of instances of plotting view that's added/removed dynamically according to incoming signals from tcp port from another process. 我有一组绘图视图实例,这些实例是根据来自另一个进程的tcp端口的传入信号动态添加/删除的。

In each instance, the user can right click to open a popup menu, invoked via wxWindow::PopupMenu . 在每种情况下,用户都可以右键单击以打开一个弹出菜单,该菜单通过wxWindow::PopupMenu调用。

If the plotting view instance is to got shutted-down dynamically while the popup menu is visible, the view instance window is closed while the popup menu is still floating. 如果要在弹出菜单可见的情况下动态关闭绘图视图实例,则在弹出菜单仍处于浮动状态时关闭视图实例窗口。 Then any GUI action crashes the application. 然后,任何GUI操作都会使应用程序崩溃。

I've been going through the APIs for wxWidgets to find a way to programmatically close the popup menu in the plotting view destructor, but with no-luck. 我一直在遍历wxWidgets的API,以找到一种以编程方式关闭绘图视图析构函数中的弹出菜单的方法,但是没有运气。

I found this forum post suggesting it's an impossible thing to close the popup menu programmatically. 我发现此论坛帖子暗示以编程方式关闭弹出菜单是不可能的。 But it's too old, so not sure if it's still valid assumption. 但是它太旧了,因此不确定是否仍然是有效的假设。


Here are the trials that failed till now : 这是迄今为止失败的试验:

  1. Trying to call SetFocus and SetFocusFromKbd on the plotting view as a way to move focus. 尝试在绘图视图上调用SetFocusSetFocusFromKbd ,以移动焦点。
  2. Generating mouse left click event and send it to the popup menu. 生成鼠标左键单击事件并将其发送到弹出菜单。
  3. Generating a keyboard event and send it to the popup menu. 生成键盘事件并将其发送到弹出菜单。

     PlottingView::~PlottingView() { cout << "Sending wxMouseEvent to the popup menu" << endl; wxMouseEvent e(wxEVT_LEFT_UP); this->GetPopupMenu()->ProcessEvent(e); wxKeyEvent ke(wxEVT_CHAR); ke.m_keyCode = WXK_DOWN; this->GetPopupMenu()->ProcessEvent(ke); ke.m_keyCode = WXK_RETURN; this->GetPopupMenu()->ProcessEvent(ke); // the rest of the destruction } 

So I will appreciate any idea to programmatically close this popup menu. 因此,对于以编程方式关闭此弹出菜单的任何想法,我将不胜感激。


Platform: 平台:
CentOS: 6.7 CentOS的:6.7
wxWidgets 2.8.12 wxWidgets 2.8.12
G++: 4.3.3 G ++:4.3.3


Edit #1 编辑#1

Note: For commenters and answers suggesting upgrading the wxWidgets version, it's a debate in my team for everyday. 注意:对于建议升级wxWidgets版本的评论者和答案,这是我团队中每天的辩论。 But the answer is still no. 但是答案仍然是。

Most of the trials has failed. 大多数试验都失败了。 But I found a workaround to stop crashing but the popup menu don't close. 但是我发现了一种解决方法,可以停止崩溃,但弹出菜单没有关闭。

The solution was to nullify the following members using their setters, so the menu callback won't access them. 解决方案是使用其设置器使以下成员无效,因此菜单回调将无法访问它们。

this->GetPopupMenu()->SetInvokingWindow(NULL);
this->GetPopupMenu()->SetEventHandler(NULL);

The best is probably to delay destroying the underlying window until PopupMenu() returns. 最好的办法可能是延迟销毁基础窗口,直到PopupMenu()返回为止。 As it is, your program logic is very convoluted because you're dispatching the event which results in closing of the window from inside PopupMenu() function and this just can't end well, even if you could use wxUIActionSimulator to close the menu (but you definitely should consider upgrading your 15 year old wxWidgets version in any case). 实际上,您的程序逻辑非常复杂,因为您正在分派事件,这会导致从PopupMenu()函数内部关闭窗口,即使您可以使用wxUIActionSimulator关闭菜单(但您绝对应该考虑升级您使用15年的wxWidgets版本)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM