Windows 键盘加速器和子窗口
我使用 C 和 Windows API 创建了一个 Windows GUI 程序,并且我希望该程序利用键盘加速器。我已经设置了一些工作正常的加速器,但是当焦点转到程序主窗口的子窗口(例如列表视图控件或状态栏控件)时,键盘加速器似乎正在转换为 WM_COMMAND 消息用于子窗口而不是主窗口。因此,当焦点位于子控件上时,我在主窗口的 WndProc 中对相应 WM_COMMAND 消息的处理将被忽略。
我应该如何解决这个问题?
I have created a Windows GUI program using C and the Windows API, and I want the program to utilise keyboard accelerators. I have set up some accelerators, which are working correctly, but when focus goes to a child Window of my program's main window, such as a list view control or a status bar control, it appears that the keyboard accelerators are being translated to WM_COMMAND messages for the child windows rather than the main window. Because of this, my handling of the appropriate WM_COMMAND messages in the main window's WndProc is ignored when the focus is on a child control.
How should I resolve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了答案。主窗口的子窗口必须进行子类化,以便可以拦截键盘加速器生成的 WM_COMMAND 消息并将其传递到父窗口。
这涉及到将控件的窗口过程更改为不同的窗口过程。备用过程通过将消息发送到父窗口来处理应拦截的消息。指向原始窗口过程的指针也必须存储在某处,以便控件可以正确运行。
可以使用 SetWindowLongPtr 更改窗口过程与 GWLP_WNDPROC。
下面是一个简单的示例,说明如何通过在控件的用户数据值 (GWLP_USERDATA) 中存储指向原始窗口过程的指针来实现此目的:
更改窗口过程并将原始过程存储在 GWLP_USERDATA 中的代码:
拦截窗口过程:
I found the answer. The child windows of the main window must be subclassed so that the WM_COMMAND messages generated by the keyboard accelerators can be intercepted and passed to the parent window.
This involves changing the window procedure of the control to a different one. The alternate procedure handles the messages that should be intercepted by sending them to the parent window. A pointer to the original window procedure must also be stored somewhere so that the control can function correctly.
The window procedure can be altered using SetWindowLongPtr with GWLP_WNDPROC.
Here is a simple example of how to do this by storing a pointer to the original window procedure in the control's user data value (GWLP_USERDATA):
The code to change the window procedure and store the original procedure in GWLP_USERDATA:
The intercepting window procedure:
另一种方法是避免对子窗口使用 TranslateAccelerator,示例代码:
如果消息不是 mainWidget 的,我们不会使用主窗口小部件的加速器表为其翻译加速器。
Another way is avoid using TranslateAccelerator for child window, sample code:
if the message is not the mainWidget's, we do not translateaccelerator for it using the accelerator table of the main widget.