Declare Function SetFocusAPI Lib "user32.dll" Alias "SetFocus" (ByVal hWnd As Long) As Long
SetFocus gives a window the input focus. The window must be owned by the thread calling the function, however -- the function will not set the focus to another program's window.
If successful, the function returns a handle to the window that previously had the input focus. If an error occured, the function returns zero (use GetLastError to get the error code).
This function behaves exactly like the SetFocus method of many VB controls. However, because this function shares its name with this method, it must be renamed to something like SetFocusAPI when you use it in a VB program, or else the compiler will give you an error. See the example for a demonstration of this.
When the form loads, immediately give button cmdGetsFocus the keyboard focus. Although an easier way to do this is through the button's SetFocus method, the API function is useful if you only have a handle to the window you want to give the focus. To use this example, you must place a button named cmdGetsFocus on a form window.
' This code is licensed according to the terms and conditions listed here.
' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Declare Function SetFocusAPI Lib "user32.dll" Alias "SetFocus" (ByVal hWnd As Long) As Long
' *** Place the following code inside a form window. ***
Private Sub Form_Load()
Dim retval As Long ' return value
' Give the input focus to cmdGetsFocus right away.
retval = SetFocusAPI(cmdGetsFocus.hWnd)
End Sub
Back to the Function list.
Back to the Reference section.
Last Modified: December 17, 2000
This page is copyright © 2000 Paul Kuliniewicz.
Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/(?/name).html