I have a window with 10 child edit controls in it. I would like to move from one edit control to other by pressing the tab key – how do I do this?
I mean, even if I could find out whether I pressed the tab-key, how do I find the next edit control to focus into? I hope I do not have to keep track of the edit controls myself as I already added them to the parent window.
PS: by “next” I mean the order I created the edit controls…
Edit: I’m on Win32 using plain C.
Edit 2: sample
#include
#define NAME "test"
LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND edit1, edit2;
switch (msg)
{
case WM_CREATE:
edit1 = CreateWindow("edit", "", WS_CHILD|WS_VISIBLE, 0, 0, 200, 50, hWnd, NULL, NULL, NULL);
edit2 = CreateWindow("edit", "", WS_CHILD|WS_VISIBLE, 250, 0, 200, 50, hWnd, NULL, NULL, NULL);
return 0;
case WM_CLOSE:
DestroyWindow(hWnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wc;
wc.style = 0;
wc.lpfnWndProc = WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wc.lpszMenuName = NAME;
wc.lpszClassName = NAME;
RegisterClass(&wc);
HWND win;
win = CreateWindow(NAME, "test", WS_OVERLAPPEDWINDOW, 0, 0, 500, 500, NULL, NULL, hInstance, NULL);
ShowWindow(win, nCmdShow);
UpdateWindow(win);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
The dialog manager does a lot of this for you, so if you don’t have a good reason for creating your own window class, you might consider creating a dialog instead.
If you’re still of a mind to roll your own, you’ll have to intercept WM_CHAR and look for VK_TAB and VK_SHIFT | VK_TAB. The dialog manager uses something called “z-order” as the tab order (http://msdn.microsoft.com/en-us/library/ms632599%28VS.85%29.aspx#zorder).
FWIW, my advice would be to not underestimate the burden you’re taking on when trying to re-create an existing facility in Windows like this. For every behavior that you know about, there are usually at least as many that you don’t. For example, how will your application behave on a pen-based device? What about accessibility extensions? Will screen readers be able to handle it properly? All of that stuff is already baked into the dialog manager.
Dialogs can be modal or modeless children of the main application window, but they can also be the main application window. That’s typically called a dialog-based app.
If you want a dialog-based application (that is, an application with a dialog as it’s main window), you’d do something like this:
The full example is probably too long to list here, but if you Google for CreateDialog, you should find some examples of this.
Yes, you can create custom controls within the dialog. Within the DIALOG portion of your .rc file, you can include something like:
You can also create a new control and add it to the dialog dynamically (I’ll let you Google for an example).
Use
GetNextDlgTabItem()if you want to cycle through the controls within a dialog in tab-order: http://msdn.microsoft.com/en-us/library/ms645495%28VS.85%29.aspxIf you’re rolling your own, then you probably want something like the
EnumChildWindows()function: http://msdn.microsoft.com/en-us/library/ms633494%28VS.85%29.aspxThis may also be useful: http://msdn.microsoft.com/en-us/library/bb775501%28VS.85%29.aspx