Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make tab key press work with win32 window that is not a dialog

Tags:

c++

c

winapi

I've created several controls in my window in the WM_CREATE message handler and I want to allow using the tab key to advance the focus through the set of controls from one to the next.

The control creation is like this:

case WM_CREATE:
{
    CreateWindowA("button", "Refresh Listview",
                  BS_MULTILINE | WS_CHILD | WS_VISIBLE, 10, 10, 70, 50,
                  hwnd, (HMENU)IDC_REFRESHLW, g_hInst, NULL);
    break;
}

When I press the tab key to change the focus to another control in the window it does nothing. Do I have to initialize it somehow?

I noticed if I use a dialog, it already automatically allows using the tab key and the tab order is the order in which you create the controls in the .rc file.

But I don't want a dialog!

like image 682
Kaije Avatar asked Jul 21 '10 12:07

Kaije


1 Answers

To get tabbing to work, you need a call to IsDialogMessage() in your message loop.

Your message loop should look something like:

HWND hwnd; // main window handle

MSG msg;
while (GetMessage(&msg, 0, 0, 0) > 0)
{
  if (!IsDialogMessage(hwnd, &msg))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
}

IsDialogMessage() works by examining the message and seeing if its a VK_TAB or related message - it then looks at the hwnd passed in to see which of its child windows has focus, and if a child window has focus, searches for other child windows with the WS_TABSTOP style, and moves focus to the next TABSTOP-enabled control in the window. The window does NOT have to be a dialog to use this function, merely have child windows that can accept focus, and have the WS_TABSTOP style.

like image 57
Chris Becke Avatar answered Oct 14 '22 07:10

Chris Becke