windows_programming_notes.nbk: Home | Index | Next Page: SetLastError | Previous Page: SetFileTime


 SetFocus

The SetFocus function sets the keyboard focus to the specified window. The window must be attached to the calling thread's message queue.

Syntax

    HWND SetFocus(
       HWND hWnd
    );

Parameters

Return Value

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 attached to the calling thread's message queue, the return value is NULL. To get extended error information, call GetLastError.

Remarks

The SetFocus function sends a WM_KILLFOCUS message to the window that loses the keyboard focus and a WM_SETFOCUS message to the window that receives the keyboard focus. It also activates either the window that receives the focus or the parent of the window that receives the focus.

If a window is active but does not have the focus, any key pressed will produce the WM_SYSCHAR, WM_SYSKEYDOWN, or WM_SYSKEYUP message. If the VK_MENU key is also pressed, the lParam parameter of the message will have bit 30 set. Otherwise, the messages produced do not have this bit set.

By using the [AttachThreadInput] function, a thread can attach its input processing to another thread. This allows a thread to call SetFocus to set the keyboard focus to a window attached to another thread's message queue.

For an example, see [Initializing a Dialog Box].

Function Information


Don't use SetFocus to change the focus in dialog boxes. Use WM_NEXTDLGCTL.


windows_programming_notes.nbk: Home | Index | Next Page: SetLastError | Previous Page: SetFileTime


Notebook exported on Monday, 7 July 2008, 18:56:50 PM Eastern Daylight Time