Making TAB key work in my win32 app

风流意气都作罢 提交于 2020-06-27 09:45:27

问题


I want to make the tab button work on my app , so when I press tab, it will change from one edit box to another, these are the edit box codes:

    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);

回答1:


The fix is quite simple. Given the fact you're handling the WM_CREATE message, rather than the WM_INITDIALOG message, it seems safe to assume that you're adding the controls to a 'standard' window, rather than a 'dialog'.

With that in mind, I expect you've got something like the following in your 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);
}

However, the documentation for IsDialogMessage states:

"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."

So, you can change your message pump to resemble the following:

/* 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);
    }
}



回答2:


As far as I remember, you should use WS_GROUP only on the first entry. All following childwindows will be added to this group. When you create a new group, you use WS_GROUP again on the first window being created. So keep the WS_GROUP on TextBox and remove it form the other windows.

You can read about this here on MSDN.

You also should use the IsDialogMessage in your message loop. You can see an example here.




回答3:


I had a similar problem and found adding the IsDialogMessage statement before TramnslateMessage and DispatchMessage (as shown in the accepted answer) fixed my problem.



来源:https://stackoverflow.com/questions/21509748/making-tab-key-work-in-my-win32-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!