Windows 键盘加速器和子窗口

发布于 2024-08-16 01:52:13 字数 229 浏览 4 评论 0原文

我使用 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 技术交流群。

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

发布评论

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

评论(2

谁的年少不轻狂 2024-08-23 01:52:13

我找到了答案。主窗口的子窗口必须进行子类化,以便可以拦截键盘加速器生成的 WM_COMMAND 消息并将其传递到父窗口。

这涉及到将控件的窗口过程更改为不同的窗口过程。备用过程通过将消息发送到父窗口来处理应拦截的消息。指向原始窗口过程的指针也必须存储在某处,以便控件可以正确运行。

可以使用 SetWindowLongPtr 更改窗口过程与 GWLP_WNDPROC。

下面是一个简单的示例,说明如何通过在控件的用户数据值 (GWLP_USERDATA) 中存储指向原始窗口过程的指针来实现此目的:

更改窗口过程并将原始过程存储在 GWLP_USERDATA 中的代码:

SetWindowLongPtr( hWnd, GWLP_USERDATA, ( LONG_PTR )SetWindowLongPtr( hWnd, GWLP_WNDPROC, ( LONG_PTR )WndProc ) );

拦截窗口过程:

static LRESULT CALLBACK WndProc( const HWND hWnd, const UINT message, const WPARAM wParam, const LPARAM lParam )
{
    switch( message )
    {
        case WM_COMMAND:
            SendMessage( GetParent( hWnd ), message, wParam, lParam );
            return 0;
        default:
        //Assume that GWLP_USERDATA has been set to the original window procedure.
            return CallWindowProc( ( WNDPROC )GetWindowLongPtr( hWnd, GWLP_USERDATA ), hWnd, message, wParam, lParam );
    }
}

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:

SetWindowLongPtr( hWnd, GWLP_USERDATA, ( LONG_PTR )SetWindowLongPtr( hWnd, GWLP_WNDPROC, ( LONG_PTR )WndProc ) );

The intercepting window procedure:

static LRESULT CALLBACK WndProc( const HWND hWnd, const UINT message, const WPARAM wParam, const LPARAM lParam )
{
    switch( message )
    {
        case WM_COMMAND:
            SendMessage( GetParent( hWnd ), message, wParam, lParam );
            return 0;
        default:
        //Assume that GWLP_USERDATA has been set to the original window procedure.
            return CallWindowProc( ( WNDPROC )GetWindowLongPtr( hWnd, GWLP_USERDATA ), hWnd, message, wParam, lParam );
    }
}
深海里的那抹蓝 2024-08-23 01:52:13

另一种方法是避免对子窗口使用 TranslateAccelerator,示例代码:

if (mainWidget() && msg.hwnd == mainWidget()->hwnd()) {
            if (TranslateAccelerator(msg.hwnd, hMainAccelTable, &msg)) {
                continue;
            }
        }
        TranslateMessage(&msg);
        DispatchMessage(&msg);

如果消息不是 mainWidget 的,我们不会使用主窗口小部件的加速器表为其翻译加速器。

Another way is avoid using TranslateAccelerator for child window, sample code:

if (mainWidget() && msg.hwnd == mainWidget()->hwnd()) {
            if (TranslateAccelerator(msg.hwnd, hMainAccelTable, &msg)) {
                continue;
            }
        }
        TranslateMessage(&msg);
        DispatchMessage(&msg);

if the message is not the mainWidget's, we do not translateaccelerator for it using the accelerator table of the main widget.

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