Question:
I am using VC++ 5.0 to develop applications based on Windows 95. I want to get the HWND of the active window, which is receiving the keyboard or mouse input and is a child window of the foreground window. I used ::getactivewindow(), but it can get only the window associated with my thread. What can I do?
Answer:
It used to be that you’d use GetFocus() to get the handle to the window that is taking user input. But this function has also been changed under Win32 to work only for windows attached to the same thread.
Here’s how to globally find the window that has the input focus, according to the Microsoft Platform SDK documentation:
“Use the GetForegroundWindow function to retrieve the handle to the window with which the user is currently working. You can associate your thread’s message queue with the windows owned by another thread by using the AttachThreadInput function.”