Thread: How can the focus be returned to previous window?

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    42

    How can the focus be returned to previous window?

    Hi there,

    When WM_SETFOCUS is trapped, we expect its WPARAM is the Handle to the window that has lost the keyboard focus. But it can NULL. In my app, it's always NULL. Why?

    Can the keyboard focus be returned to the previous another application window when the mouse is moved out of your app window?

    Thanks in advance,

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    When you recieve the focus, try getting the previous window in the Z-order from yours - it may be the window that lost the focus.

    GetWindow(hwnd, GW_HWNDPREV)

    gg

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    42
    Thanks gg,

    It seems this is a good idea. The previous window handle is retrieved, but when using SetFocus to set focus back to it, it seems not work, because SetFocus returns NULL, and MSDN explains

    Return Values
    If the function succeeds, the return value is the handle to the window that previously had the keyboard focus. If the hWnd parameter is invalid or the window is not associated with the calling thread's message queue, the return value is NULL. To get extended error information, callGetLastError.

    I don't understand "If the window is not associated with the calling thread's message queue, the return value is NULL.". What does it mean "the window is not associated with the calling thread's message queue"? And what kind of a "window is associated with the calling thread's message queue"?

    Thanks again,

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Each thread can have a message queue that can pump messages for a window created by that thread.

    Try SetForegroundWindow().

    gg

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    42
    Thanks a lot gg,

    Correct me if I'm wrong.

    An application can create multiple processes, each process can have multiple threads of execution, and each thread can create windows.

    SetFocus can only change the keyboard focus between windows created by a thread; it can not change the focus to a window created by other thread. SetForegroundWindow can only change the focus between windows beloged to the same process. It can not change focus to a window created by another process.

    Is there an API within MS Windows, you can change the focus to a window created by a different APPLICATION?

    Thanks,

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> SetForegroundWindow can only change the focus between windows beloged to the same process.
    That's not true. There are rules, but they aren't that restrictive.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ensuring That Main Child Window Receives Focus
    By SMurf in forum Windows Programming
    Replies: 3
    Last Post: 08-28-2006, 05:32 AM
  2. Problem with creating new window, from another window
    By Garfield in forum Windows Programming
    Replies: 6
    Last Post: 01-11-2004, 02:10 PM
  3. dont want to use all params
    By stallion in forum Windows Programming
    Replies: 2
    Last Post: 02-18-2003, 08:10 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM