[Win32] about SetFocus

Compiler is MSVC++ express edition.
No MFC and other helper lib-s for gui controls.
I have created simple window with couple of child edit-boxes constrols within.
I need to set keyboard focus to those edit-boxes when i press "tab" or "return"
key from one to other EB.
How do i aproach this?
What functions do i need to use to set WindProc for each EB, so i can respond to
keyboard messages when it got focus?
Or can i manage this some other way?

this is how i create EB (in main window WM_CREATE message):
1
2
3
4
5
ghwndEditBox = CreateWindow(
		L"edit", L"100", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_NUMBER,
		20, 20, 
		40, 25, 
		hwnd, (HMENU)tmpId, NULL, NULL);
You can catch WM_KEYDOWN messages. In a WM_KEYDOWN message, wParam is the virtual keycode of the button that was pressed, for tab this is VK_TAB.

PS: Why do you use L"edit"? If you want to use Unicode, you should just use the TEXT macro.

So basically, what you want to do is, to be able to tab through your edit boxes?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
case WM_KEYDOWN:
       switch(wParam)
       {
              case VK_TAB:
              SetFocus(HWEditBoxes[i]);
              ++i;
              i = i % NumEditBoxes;
              return 0;
              
       }
       break;
        
case WM_KILLFOCUS:
        if(hWnd == HWMyWindow) 
        i = 0;
        return 0;


This is under the assumption you have a persistent int variable i, an array of the editboxes HWEditBoxes, the number of those edit boxes NumEditBoxes, as well as a handle to your main window HWMyWindow.
It doesn't work, as soon as one of edit-boxes get keyboard focus main window doesn't
respond to WM_KEYDOWN so i can't switch to other edit box.
Do i need to create WindProc for each EB for this to wrork?
I have put your code into "main" window WindProc function.
Thanks greatly on your help and time.
How does one set WindProc for specific control?
I know for main window i have to set and register WNDCLASS struct in witch i pass my
WindProc, but what about predefined class like edit box, is there some function to set this?
Or if is there some other simpler solution witch i can use?
Thanks.
Uh damn, yeah of course. You can get the old and set the new window proc for default controls with the GetWindowLongPtr and SetWindowLongPtr functions respectively.

http://msdn.microsoft.com/en-us/library/ms633585%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/ms644898%28VS.85%29.aspx

I am not sure if there is a simpler solution, though it wouldn't surprise me considering that it's a very common task.
Last edited on
Your main window must have been created with WS_TABSTOP style passed to CreateWindow().
You mean set that flag to "main" window? I tried that and it doesn't work but i have found
solution, set each edit box same WndProc:
1
2
for(USHORT i = 0; i < NumEdits; ++i)
    SetWindowSubclass(ghwndEdit[i], EditControlsProc, 0, 0);

witch handles only WM_KEYDOWN (tab & return).
Thanks for your help.
Windows can handle this for you. what you need to do is put an extra call in your message loop:

1
2
3
4
5
6
7
8
9
10
11
12

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

// where hWndMain is your main parent window.


Then on your edit boxes, buttons and all that, add the style WS_TABSTOP. e.g. WS_CHILD | WS_VISIBLE | WS_TABSTOP.

Windows will then handle any TAB, SHIFT+TAB that you do and automatically focus on the next control in the window. ( the tab order is the order in which you created the controls.)
Last edited on
That's it. Thanks for your time.
Topic archived. No new replies allowed.