Subclassing an ActiveX ControlCreated in VB

Subclassing an ActiveX control that we create through VB is similar to subclassing a third-party control. However, we must overcome one small hurdle first. The problem is that the UserControl module’s hwnd property is not visible outside of the UserControl module.

You can overcome this problem in two ways. First, you can use the FindWindowEx function to get the handle to the control. This function is declared in this manner in VB:

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
		(ByVal hWnd1 As Long, ByVal hWnd2 As Long, _
		ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

The function has the following parameters:

hWnd1

The hWnd of the parent window to the window that you want to find. The function uses this parameter as a starting point for searching for the target window. If Null, the function uses the desktop as the parent window.

hWnd2

The hWnd of any child windows to the window specified by the hWnd1 parameter. If a valid child window handle is provided, this function starts searching through all the windows that are children to the window specified by this parameter.

lpsz1

The class name or class atom of the window that we are searching for.

lpsz2

The caption of the window that we are searching for.

If this function succeeds, the hWnd of the window is returned; otherwise, a zero is returned, indicating failure.

Example 6-1 shows the code to find the hWnd of a VB-created ActiveX control.

Example 6-1. Finding ...

Get Subclassing and Hooking with Visual Basic now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.