Share via


Window Focus

In Win32, each thread of execution can set or get the focus only to windows created by the current thread. This prevents applications from interfering with each other. One application’s delay in responding cannot cause other applications to suspend their response to user actions, as often happens in Windows 3.x.

Consequently, the following API functions work differently under Win32:

GetActiveWindow( VOID ) SetActiveWindow( HWND )
GetCapture( VOID ) SetCapture( HWND )
GetFocus( VOID ) SetFocus( HWND )
ReleaseCapture( VOID )

The Get functions in the preceding list can now return NULL, which could not happen in Windows 3.x. Therefore, it’s important to test the return value of GetFocus before using it. Instead of returning the window handle of another thread, the function returns NULL. For example, you call GetFocus and another thread has the focus. Note that it’s possible for a call to GetFocus to return NULL even though an earlier call to SetFocus successfully set the focus. Similar considerations apply to GetCapture and GetActiveWindow.

The Set functions can only specify a window created by the current thread. If you attempt to pass a window handle created by another thread, the call to the Set function fails.