Click to Rate and Give Feedback
MSDN
MSDN Library
User Interface
User Input
Keyboard Input
Functions
 SetFocus Function
SetFocus Function

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

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

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

Minimum DLL Versionuser32.dll
HeaderDeclared in Winuser.h, include Windows.h
Import libraryUser32.lib
Minimum operating systems Windows 95, Windows NT 3.1

See Also

Keyboard Input, AttachThreadInput, GetFocus, WM_KILLFOCUS, WM_SETFOCUS, WM_SYSCHAR, WM_SYSKEYDOWN, WM_SYSKEYUP
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Do not call SetFocus in a dialog window, except in WM_INITDIALOG      Gideon7   |   Edit   |  

Note: Do not call SetFocus to change a control's focus in a dialog window, except when handling the WM_INITDIALOG message.

To explicitly set the focus to a dialog's control call SendMessage(hDialog, WM_NEXTDLGCTL, (WPARAM)hwndCtrl, TRUE). The WM_NEXTDLGCTL message notifies the dialog manager that the focus has changed.

The exception is the initial WM_INITDIALOG message. When you receive WM_INITIDIALOG call SetFocus on the desired control's hWnd. You must return FALSE to notify the dialog manager that you set the initial focus.

When handling other dialog messages if you use SetFocus (and not WM_NEXTDLGCTL) it prevents the dialog manager from updating its internal bookkeeping. For example, it might cause the dialog manager to display the wrong default pushbutton or fail to select the text of an edit control. For details see Raymond Chen's book The Old New Thing, page 227, "How to set focus in a dialog box." (Or see his blog.)

Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker