How to Get hWnd Handle for a Control by Using GetFocus API

ID Number: Q71799

1.00

WINDOWS

Summary:

Microsoft Visual Basic does not provide access to the Microsoft

Windows handle (hWnd) for any control other than the form. By using

the Windows API GetFocus function, you can retrieve the handle to most

Visual Basic controls.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

Visual Basic provides a hWnd property for the form, but none of the

provided controls has this property. By using the Windows API GetFocus

function and the Visual Basic SetFocus method, you can obtain the

hWnd for most Visual Basic controls. With the exception of the frame

and label controls, neither of these controls can receive the focus

for user input and do not support the Visual Basic SetFocus method.

The Visual Basic SetFocus method allows the changing of the input

focus to a specific control, rather than waiting for the user to

select the desired control manually. The following code will change

the focus to the command button Command1 on the form:

Command1.SetFocus

The GetFocus API function requires no parameters. The GetFocus API

function returns an integer value that represents the handle to the

control. Use GetFocus to get a handle to the control that currently

has the focus:

hWd% = GetFocus() ' HWnd is reserved word in Visual Basic, so you

' must use a different name, such as hWd%

Most Windows API functions require the handle (hWnd) of the window

that is to be acted upon.

For examples of using the Windows API GetFocus function in Visual

Basic, query on the following word:

GETFOCUS

Additional reference words: 1.00