c++ - 让 TAB 键在我的 win32 应用程序中工作

标签 c++ winapi codeblocks

我想让标签按钮在我的应用程序上工作,所以当我按下标签时,它会从一个编辑框变为另一个,这些是编辑框代码:

    case WM_CREATE:

    TextBox = CreateWindow("EDIT",
                            "",
                            WS_BORDER|WS_CHILD|WS_VISIBLE|WS_EX_LAYERED|WS_TABSTOP|WS_GROUP,
                            60,50,200,20,
                            hwnd,NULL,NULL,NULL);
    DataBox = CreateWindow("EDIT",
                            "",
                            WS_BORDER|WS_CHILD|WS_VISIBLE|WS_TABSTOP|WS_GROUP,
                            60,72,200,20,
                            hwnd,NULL,NULL,NULL);
    MotivBox = CreateWindow("EDIT",
                            "",
                            WS_BORDER|WS_CHILD|WS_VISIBLE|WS_TABSTOP|WS_GROUP,
                            60,92,200,20,
                            hwnd,NULL,NULL,NULL);
    PretBox = CreateWindow("EDIT",
                            "",
                            WS_BORDER|WS_CHILD|WS_VISIBLE|WS_TABSTOP|WS_GROUP,
                            60,112,200,20,
                            hwnd,NULL,NULL,NULL);

最佳答案

修复非常简单。考虑到您正在处理 WM_CREATE 消息而不是 WM_INITDIALOG 消息,可以安全地假设您正在将控件添加到“标准”窗口而不是“对话框”。

考虑到这一点,我希望您的 winmain 中有如下内容:

/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
    /* Translate virtual-key messages into character messages */
    TranslateMessage(&messages);
    /* Send message to WindowProcedure */
    DispatchMessage(&messages);
}

但是,IsDialogMessage 的文档指出:

"Although the IsDialogMessage function is intended for modeless dialog boxes, you can use it with any window that contains controls, enabling the windows to provide the same keyboard selection as is used in a dialog box. When IsDialogMessage processes a message, it checks for keyboard messages and converts them into selection commands for the corresponding dialog box. For example, the TAB key, when pressed, selects the next control or group of controls, and the DOWN ARROW key, when pressed, selects the next control in a group.

Because the IsDialogMessage function performs all necessary translating and dispatching of messages, a message processed by IsDialogMessage must not be passed to the TranslateMessage or DispatchMessage function."

因此,您可以将消息泵更改为如下所示:

/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
    /* Translate virtual-key messages into character messages */
    if (IsDialogMessage(hwnd, &messages) == 0)
    {
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }
}

关于c++ - 让 TAB 键在我的 win32 应用程序中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21509748/

相关文章:

c++ - 如何轻松检测字符串中的utf8编码?

c++ - 如何使用指针 C++ 将二维数组中的元素复制到一维数组中

windows - 如何在 Windows 的控制台输出上中断(设置断点)?

c++ - 将 GTKmm 添加到 CodeBlocks - Windows

c++ - 为什么虚幻引擎中的变量为空?

c++ - 在Linux上使用C中的输入功能,无需按Enter键

Windows 资源管理器获取所选项目的列表并将其传递给另一个进程

multithreading - Win32 : ReadFileEx() on STD_IN_HANDLE blocks, 为什么?

c++ - 以数学方式将 float 四舍五入到小数点后 N 位

c++ - 代码::在 Windows 中以固定精度阻止 printf double