Setting focus

This is a discussion on Setting focus within the Windows Programming forums, part of the Platform Specific Boards category; Okay my current problem is as follows: Each window in my MDI interface is created with two controls. A richedit ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    132

    Setting focus

    Okay my current problem is as follows:

    Each window in my MDI interface is created with two controls. A richedit control and an editbox. What I want to do is whenever the focus is set on the richedit control, I want to set the focus to the editbox instead (keep focus off of the richedit). I store both HWNDs for the controls when they are created, and I tried when WM_SETFOCUS comes in that I get the HWND of the control by using GetFocus() and then comparing the returned HWND with that of the RichEdit (if they match then SetFocus() to the editbox). However, this doesn't do anything.

    Any ideas?

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    SetFocus() is dodgy, I haven't worked out how to use it properly. I do something very iffy:
    Code:
    mouse_event(|MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN , dx , dy , NULL , NULL);
    mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTUP , dx , dy , NULL , NULL);
    You can get dx and dy using GetWindowRect() with the control's HWND.

    This method may not seem like the most logical, but it works.

    However, if anyone knows how to get SetFocus() working, please tell.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Have you tried EnableWindow()?
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I tried it in all sorts of combinations with SetFocus(), SetActiveWindow(), and SetForegroundWindow(), but still doesn't work.

    According to the Win32 Programmer's Reference,
    To allow the user to easily identify the active window, Windows places it at the top of the Z order and changes the color of its title bar and border to the system-defined active window colors.
    And:
    The user activates a top-level window by clicking it (or one of its child windows), or by using the ALT+ESC or ALT+TAB key combination. An application activates a top-level window by calling the SetActiveWindow function.
    This implies that SetActiveWindow() should have the same effect as clicking in a window. On my machine, and clearly on other people's machines, this does not work.

    Any ideas?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    ok, made a little test app.

    simply launches a dialog with ok, cancel and an edit control.

    I used SetFocus() and lo and behold, the edit control indeed got focus, without SetFocus the ok button recieves default focus.
    Code:
    BOOL CALLBACK DlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
    {
    	HWND hEdit = GetDlgItem(hDlg, IDC_EDIT1);
    
    	switch(Msg)
    	{
    	case WM_INITDIALOG:
    
    		SetDlgItemText(hDlg, IDC_EDIT1, L"This should be active.");
    		SetFocus(hEdit);
    
    		return false;
    
    	case WM_COMMAND:
    		switch(LOWORD(wParam))
    		{
    		case IDOK:
    		case IDCANCEL:
    			EndDialog(hDlg, 0);
    			return true;
    		}
    		break;
    	}
    	return false;
    }
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    132
    Okay, I understand all this. However, how do I compare HWNDs to see if its the proper one...

    I've tried using the following:

    Code:
    HWND hFocus = GetFocus();
    if( hFocus == hRich ) { SetFocus(hEdit); }
    but it doesn't work. At least not for me.

  7. #7
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    The SetFocus function seems to work best with dialogs and their child controls. Where as on regular main windows the function call is sporatic at best.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    132
    Is there a function that isn't so sporatic for normal types of windows?

  9. #9
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    It would seem to be no for now. If I can find a function that works with main windows then I'll have to let you know. Otherwise I have just tested two of the funtions and they will not work for the life of them. Damn Win16 functions! It would be nice if Microsoft would create something that would work all the time. Damn William Gates III!
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    I don't seem to have any trouble with this.

    try setting the MDI child as having focus first (is active) then setfocus on the edit.

    Or try SetWindowPos() to change the MDI childs Z order (top) then, as the parent has focus (is active), it may now allow the child control to get focus.




    Try playing with the TAB stop and group settings (may help who knows?)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  11. #11
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Oh, now I understand. I thought we were talking about top-level windows. I haven't tried this with MDIs or controls, just top-levels.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Setting window focus
    By scrub05 in forum C++ Programming
    Replies: 1
    Last Post: 01-12-2005, 11:59 AM
  2. Setting a control to have startup focus
    By bennyandthejets in forum C# Programming
    Replies: 1
    Last Post: 09-19-2004, 09:14 PM
  3. Setting focus in an edit box
    By nasec in forum Windows Programming
    Replies: 2
    Last Post: 04-28-2003, 12:33 PM
  4. setting focus on a dialog box control
    By face_master in forum Windows Programming
    Replies: 2
    Last Post: 05-14-2002, 04:11 PM
  5. Setting focus on a window
    By pinkcheese in forum Windows Programming
    Replies: 5
    Last Post: 04-02-2002, 07:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21