Compartilhar via


SetFocus (Compact 2013)

3/28/2014

This function sets the keyboard focus to the specified window. All subsequent keyboard input is directed to this window. The window, if any, that previously had the keyboard focus loses it.

Syntax

HWND SetFocus(
  HWND hWnd 
); 

Parameters

  • hWnd
    [in] Handle to the window that will receive the keyboard input. If this parameter is NULL, keystrokes are ignored.

Return Value

The handle to the window that previously had the keyboard focus indicates success. NULL indicates that the hWnd parameter is invalid or the window is not associated with the calling thread's message queue. 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 ALT 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.

Do not use the SetFocus function to set the keyboard focus to a window associated with another thread's message queue. There is one exception. If a window on one thread is the child of the window on the other thread, or if the windows are siblings of the same parent, the thread associated with one window can set the focus to the other window even though it belongs to a different thread.

Requirements

Header

winuser.h

Library

Caret.lib,
Foregnd.lib

See Also

Reference

Keyboard Functions