hWnd Property

Applies to:  Window object
See also:  

read-only

Returns the window's handle, as assigned by the operating system.

Syntax:

object.hWnd

The hWnd syntax has these parts:

Part Description
object The name of a Window object.

Remarks:

If a window is closed, a Window object attached to that window is not automatically destroyed.  It is possible to check whether a Window has been closed by checking its hWnd property.  This property is set to zero when the window is closed.

The hWnd property can be used when calling Win32 API functions which take an hWnd as a parameter.  As this is the default property for a Window object, it is not necessary to reference the hWnd property explicitly.

Therefore, the following to lines of code (which use the Win32 API SetActiveWindow) are equivalent:

SetActiveWindow objMyWindow
SetActiveWindow objMyWindow.hWnd

It is possible for more than one Window object to share the same hWnd.  For example, each call to WrapWindow will return a distinct Window object instance, even though each instance will be attached to the same hWnd.  Therefore, to check whether two Window objects relate to the same actual window - check whether the hWnd property is the same for both objects.

Therefore, the following code is inadvisable

If MyWindow Is MyOtherWindow Then
  'Perform some action
End If

The following is preferable:

If MyWindow.hWnd = MyOtherWindow.hWnd Then
  'Perform some action
End If

 

Example:

The FindControlFromWindow function, in the "Control Highlighting" example, iterates through all of the controls on a form, to find which one corresponds to a Window object.

 

Home Copyright and Disclaimer