
SetFocus
The SetFocus function sets the keyboard focus to the specified window. The window must be associated with the calling thread's message queue.
SetFocus函数设置键盘焦点到指定窗口.这个窗口必须要和调用线程的消息队列是相连的.
HWND SetFocus(
HWND hWnd // handle to window to receive focus(接收焦点的窗口的句柄)
);
Parameters
参数
- hWnd
- Handle to the window that will receive the keyboard input. If this parameter is NULL, keystrokes are ignored.
将要接收键盘输入的窗口的句柄.如果此参数为NULL,键击将被忽略.
Return Values
返回值
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 associated with the calling thread's message queue, the return value is NULL. To get extended error information, call GetLastError.
如果函数执行成功,返回先前拥有键盘焦点的窗口句柄.如果hWnd参数无效或窗口与调用线程的消息队列无关联,返回NULL.要获取更准确的错误信息,调用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.
SetFocus函数向失去键盘焦点的窗口发送一个WM_KILLFOCUS消息,向得到焦点的窗口发送WM_SETFOCUS消息. 同时使获得焦点的窗口或其父窗口成为活动窗口.
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.
如果一个窗口是活动的但没有焦点,任何按键将产生WM_SYSCHAR,WM_SYSKEYDOWN,或WM_SYSKEYUP消息.如果VK_MENU键也被按下,消息的lParam参数将设置第30位.否则, 产生的消息不进行此位设置.
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 associated with another thread's message queue.
使用AttachThreadInput函数,一个线程可将输入处理连接到其他线程。这使得线程可以调用SetFocus函数为一个与其他线程的消息队列相关的窗口设置键盘焦点。
Windows CE: 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. In this case, there is no need to call AttachThreadInput first.
Windows CE:不使用SetFocus函数为一个与其他线程的消息队列相关的窗口设置键盘焦点。但有一个例外。若一个线程的窗口是另一线程的子窗口,或这些窗口是具有同一父窗口的兄弟窗口,则与一个线程关联的窗日可以为其他窗口设置焦点,尽管该窗口属于一个不同的线程。在这种情况下,就不必先调用AttachThreadlnpUt函数。
QuickInfo
快速信息
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winuser.h.
Import Library: Use user32.lib.
Windows NT:3.1及以上版本;
Windows:95及以上版本:
Windows CE:1.0及以上版;
头文件:winuser.h;
库文件:user32.lib。

