处理对话框上的键盘输入 - Windows [英] Handling keyboard input on a dialog - Windows

查看:19
本文介绍了处理对话框上的键盘输入 - Windows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个基于非模态对话框的应用程序.我正在使用 Visual Studio 资源编辑器来构建对话框并创建应用程序.到目前为止,一切都进展顺利.但是,在处理键盘输入方面,我遇到了困难.我的研究表明,windows 在到达对话框回调过程之前捕获键盘输入,以实现使用键盘移动焦点的能力.

I am programming an application that is non-modal dialog based. I am using the Visual Studio resource editor to build the dialog and create the application. Everything is going great so far. However, I have reached a brick wall when it comes to handling keyboard input. My research has shown that windows captures keyboard input before it reaches the dialog callback procedure to implement the ability to move the focus using the keyboard.

我创建了一个简单的对话框来测试这个,当我有一个没有任何控件的对话框时,一切都像我期望的那样工作.当我添加控件时,它停止工作.

I created a bare bones dialog to test this and, when I have a dialog without any controls on it, everything works as I would expect it to. When I add a control, it stops working.

这是来自裸骨项目的代码:

Here is the code from the bare bones project:

#include <Windows.h>
#include <CommCtrl.h>
#include <tchar.h>
#include "resource.h"

INT_PTR CALLBACK DialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM  lParam);
HWND dlghandle;
int WINAPI _tWinMain(HINSTANCE hInst, HINSTANCE h0, LPTSTR lpCmdLine, int nCmdShow)
{
HWND hDlg;
MSG msg;
BOOL ret;


hDlg = CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_DIALOG1), 0, DialogProc, 0);
ShowWindow(hDlg, nCmdShow);
dlghandle = hDlg;
while ((ret = GetMessage(&msg, 0, 0, 0)) != 0) {
    if (ret == -1)
        return -1;
    if (!IsDialogMessage(hDlg, &msg))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}
return 0;
}

INT_PTR CALLBACK DialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
 {
    switch (uMsg)
{
case WM_KEYDOWN:
    MessageBox(NULL, L"Test", L"Test", MB_OK);
    break;
case WM_CLOSE:
    DestroyWindow(hDlg);
    break;
case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
    break;
}
return 0;
}

我有哪些选择?

推荐答案

出现的第一个键盘输入是您的消息循环,因此只需检查您正在寻找的按键,然后再将其传递给 IsDialogMessage()/TranslateMessage()/DispatchMessage().例如:

The first place keyboard input appears is your message loop, so just check for the keypress you're looking for before passing it off to IsDialogMessage() / TranslateMessage() / DispatchMessage(). For example:

while ((ret = GetMessage(&msg, 0, 0, 0)) != 0) {
    if (ret == -1)
        return -1;

    if (msg.message == WM_KEYDOWN && msg.wParam == 'Q')
    {
        // trap and handle the Q key no matter which control has focus
    }
    else
    if (!IsDialogMessage(hDlg, &msg))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

如果你愿意,你甚至可以将它包装在一个函数中以使其更整洁,例如:

If you wanted you could even wrap this in a function to make it neater, e.g.:

#define MYMSG_DOSOMETHING   (WM_APP + 1)

BOOL MyIsDialogMessage(HWND hDlg, MSG* pMsg)
{
    if (pMsg->message == WM_KEYDOWN && pMsg->wParam == 'Q')
    {
        PostMessage(hDlg, MYMSG_DOSOMETHING, 0, 0);
        return TRUE;
    }
    return IsDialogMessage(hDlg, pMsg);
}


// then your message loop would be:

while ((ret = GetMessage(&msg, 0, 0, 0)) != 0) {
    if (ret == -1)
        return -1;

    if (!MyIsDialogMessage(hDlg, &msg))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

这篇关于处理对话框上的键盘输入 - Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆