wxWidgets 和上下文菜单

发布于 2024-08-23 15:06:13 字数 831 浏览 4 评论 0原文

我正在尝试将上下文菜单添加到应用程序中的(大型)自定义控件中,但在何时创建它们方面遇到了许多问题。

在 3 种情况下,需要通过单击鼠标右键、按下菜单键以及 Shift + F10 创建上下文菜单(除非我忘记了...)。在所有情况下,菜单都是通过 ContextMenu 方法创建和显示的。

鼠标右键可以执行一些其他特殊操作,因此不应总是创建上下文菜单。我正在处理各种鼠标事件并根据需要调用 ContextMenu,这一切都运行良好。

问题出在键盘生成的上下文菜单上。我在我的控件中为 EVT_CONTEXT_MENU 设置了一个处理程序,然后它只调用 ContextMenu。在很多情况下,这两个组合键似乎都被忽略了。有时 Shift+F10 工作正常,但在同样的情况下,菜单键以某种方式调用我的鼠标右键单击处理程序,导致不正确的行为...

对于菜单键,我也尝试处理按键事件,但菜单键从未出现尽管所有其他键似乎都工作正常,但在 wxWidgets 中触发这些方法...

  1. 为什么 wxWidgets 会以某种方式忽略关键方法。只要控件具有焦点,它们就应该起作用。当他们开始“工作”时,他们会继续工作,直到再次失去焦点,但我不确定他们为什么不开始工作,或者是什么导致他们开始工作(我不处理任何其他地方的上下文菜单或关键事件)我的测试应用程序)。

  2. 菜单键事件如何进入我的 EVT_RIGHT_UP 处理程序而不是按键事件或上下文菜单事件?

    菜单键事件如何进入

我认为我对 wxWidgets 如何处理这些键的理解从根本上是错误的,但是在查看文档和网上一段时间后,我没有找到有关我的问题的任何信息。

我在 Windows Vista 上使用 wxMSW 2.8.10。

I'm trying to add context menus into a (large) custom control in my application but have run into a number of issues with when to create them.

There are 3 cases when a context menu needs to be created (unless I forgot one...) on a right mouse click, when the menu key is pressed and for Shift + F10. In all cases the menu is created and displayed by a ContextMenu method.

The right mouse does some other special stuff and so shouldn't always create a context menu. I'm handling the various mouse events and call ContextMenu as needed, which is all working nicely.

The problem is with the keyboard generated context menus. I've set a handler in my control for EVT_CONTEXT_MENU which then just calls ContextMenu. In a lot of cases both key combinations appear to just be ignored. Sometimes the Shift+F10 works correctly, but in the same cases the menu key is somehow invoking my right mouse click handlers, resulting in incorrect behaviour...

For the menu key I also tried handling the key events, but the menu key never seems to trigger these in wxWidgets although all the other keys seem to work fine...

  1. Why are the key methods somehow ignored by wxWidgets. They should work as long as the control has focus. When they start "working" they keep working until it looses focus again, but im not sure why they don't to start with or what is causing them to start working (I don't handle any context menu or key events anywhere else in my test app).

  2. How is the menu key event getting into my EVT_RIGHT_UP handler instead of the key events or context menu events?

I assume it is something fundamentally wrong with my understanding of how wxWidgets handles these keys, however having looked around the documentation and on the net for some time I haven't found any info regarding my issues.

I am using wxMSW 2.8.10 on Windows Vista.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初相遇 2024-08-30 15:06:13

wxWidgets 是一个用于抽象平台差异的库,例如哪些鼠标或按键事件应导致出现上下文菜单。可能存在差异,例如它是在鼠标按下时出现,还是在鼠标松开时出现,甚至(在 Mac 上)在 Ctrl + 单击单个鼠标按钮时出现。

因此,您不应该直接处理鼠标和键盘事件,而应该处理 wxContextMenuEvent 相反。请注意,它将在鼠标和键盘事件之后调用:

请注意,如果 wxContextMenuEvent::GetPosition 返回 wxDefaultPosition,这意味着该事件源自键盘上下文按钮事件,您应该自己计算合适的位置,例如通过调用 wxGetMousePosition()。

请注意,您还可以抑制某些鼠标事件的事件并为其他事件生成该事件:

在 Windows 上按下键盘上下文菜单按钮时,首先发送默认位置的右键单击事件,如果不处理该事件,则发送上下文菜单事件。因此,如果您处理鼠标事件并且发现上下文菜单事件处理程序没有被调用,您可以为鼠标右键事件调用 wxEvent::Skip() 。

希望用此事件的处理程序替换上下文菜单的当前代码将会起作用。对于所有也会导致上下文菜单出现的鼠标事件,您需要调用 event.Skip() ,也许不这样做是您现在获得不一致结果的原因。

wxWidgets is a library to abstract away platform differences, like which mouse or key events should result in a context menu to appear. There may be differences like whether it appears on mouse down, or on mouse up, or even (on the Mac) on Ctrl+click of the single mouse button.

You should therefore not handle mouse and keyboard events directly, but handle the wxContextMenuEvent instead. Note that it will be called after both mouse and keyboard events:

Note that if wxContextMenuEvent::GetPosition returns wxDefaultPosition, this means that the event originated from a keyboard context button event, and you should compute a suitable position yourself, for example by calling wxGetMousePosition().

Note that you can also suppress the event for some mouse events and have it generated for others:

When a keyboard context menu button is pressed on Windows, a right-click event with default position is sent first, and if this event is not processed, the context menu event is sent. So if you process mouse events and you find your context menu event handler is not being called, you could call wxEvent::Skip() for mouse right-down events.

Hopefully replacing your current code for context menus with handlers for this event will work. For all mouse events that should also cause the context menu to appear you would need to call event.Skip(), maybe not doing this is the reason you are getting inconsistent results now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文