locked
Keyboard input doesn't work in dialog box created with CreateWindowEx RRS feed

  • Question


  • Hello.  I've made a dialog box using CreateWindowEx for the main window and for all child controls (ie. not using the functions to create a dialog box, because I don't want to deal with dialog units).  The problem is that the keyboard input doesn't behave like a normal dialog box.

    When the window is first created, the parent receives WM_CHANGEUISTATE enabling the input.  Furthermore, the top-level window has the keyboard focus.  The accelerators on the buttons are properly underlined, the default button is properly highlighted.  But the arrow keys, enter key (for the default button), and accelerator keys (underlined letters of buttons) do nothing.  But if I push Tab once, the first control with WS_TABSTOP gets the keyboard focus, and then suddenly everything works:  arrow keys, enter key, and accelerator keys.

    I am using IsDialogMessage properly in my main loop (which is why it works after I push Tab).  I just don't understand why I have to push Tab before the enter and accelerator keys work.  In a normal dialog box, these work all the time, whether any particular control has keyboard focus.  So I'm wondering, what else should I be checking for?  Thanks!

    (PS: please don't answer that I should use CreateDialog instead of CreateWindow.  This is avoiding the subject and is not useful to me.)
    Monday, August 18, 2008 8:58 AM

Answers

  • In case someone searching the internet stumbles onto this question, I solved it by doing this:

    1. Set cbWndExtra to DLGWINDOWEXTRA when registering the parent window class.
    2. When the parent window receives WM_NCCREATE, call SetWindowLong on the parent window with DWL_DLGPROC to set a dialog procedure.
    3. In that dialog procedure, if the message is WM_INITDIALOG, return 1; otherwise return 0.
    4. Call DefDlgProc instead of DefWindowProc from the parent window procedure.

    (The response to WM_INITDIALOG could be changed to set the default focused item when Alt is pressed).

    • Marked as answer by yessopie Tuesday, August 19, 2008 3:46 AM
    Tuesday, August 19, 2008 3:45 AM