Thread: I got subclassing to work, but....

  1. #1
    Registered User
    Join Date
    Mar 2008
    Palm Bay, FL

    I got subclassing to work, but....

    Yes another question about subclassing, however I got it to work for one situation but not the one I was hoping for.

    I created a dialog box that is the only window, no parent window. I have an edit control that I would like to activate a button on the page when I press enter while the edit box has the focus. I searched this forum and Googled subclassing, read up on it and typed some lines in my program but it wouldn't work...nothing. Can't even get a MessageBox to come up from inside the subclassed window procedure.

    However, I made a test program this morning with an edit control on the main window page and subclassing worked like a champ...1st time out. I used the same lines of code, etc, as the dialog box only program.

    Why is this?



  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Sounds like it isn't sub-classing at all, but whether or not you have a default button specified on the dialog.

    You can use DM_SETDEFID message, or the BS_DEFPUSHBUTTON style, or the DEFPUSHBUTTON resource-definition statement.

    However, if your edit has the "want return" style, then I don't think the default button is activated.


  3. #3
    Registered User
    Join Date
    Mar 2008
    Palm Bay, FL
    Okay, I found the answer in

    For archival purposes here is the solution. You have to trap WM_GETDLGCODE in the subclassed edit procedure and return DLGC_WANTALLKEYS. Here is the code from my subclassed edit procedure and the reason given from the news group:

    LRESULT CALLBACK NewEditProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    	switch (msg)
    	case WM_KEYDOWN:
    		if (wParam == VK_RETURN)
    			MessageBox (hWnd, TEXT ("Got it!"), TEXT ("NewEditProc"), MB_OK);
    			return 0;
    	case WM_GETDLGCODE:  // *** SEE NOTE BELOW  ***
    		return DLGC_WANTALLKEYS;
    	return CallWindowProc (g_OldDlgProc, hWnd, msg, wParam, lParam);

    > Thanks John for the save. The multiline style does allow the return to
    > trapped. So what happens to the VK_RETURN keystroke is a single line
    > box? Where is it handled?

    The dialog box eats it. See WM_GETDLGCODE message - the dialog sends it
    to the control having focus when it gets a keyboard message for certain
    special keys, such as Tab, Return and Arrows. If the control does not
    express interest in those messages, dialog box does not forward them to
    the control but instead implements standard behavior (e.g. when Return
    is pressed, it "pushes" the default button).

    Multiline edit control returns DLGC_WANTALLKEYS flag in response to
    WM_GETDLGCODE, single-line edit control does not. In your subclassing
    window proc, you can handle WM_GETDLGCODE and set DLGC_WANTALLKEYS flag
    regardless of whether the underlying control is a single-line or
    multiline. Then you'll always get your Return.
    With best wishes,
    Igor Tandetnik


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM