Subclassing a Third-PartyActiveX Control
This is the easiest of the four types of subclassing discussed in this chapter. Subclassing a third-party ActiveX control is exactly the same as subclassing a VB form. There is no difference because the system sees both the control and the form as windows. Using SetWindowLongPtr, you would subclass the control just like you would any other window:
Private Sub Form_Load( )
gCtrlWndProc = SetWindowLongPtr(Button1.GetHwnd, GWL_WNDPROC, AddressOf CtrlProc)
End SubYou set up the subclass function just like you would any other subclass function: by passing the window message on to the original window procedure by way of CallWindowProc, as the following subclass function indicates:
Public Function CtrlProc(ByVal hWnd As Long, ByVal uMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
'Do subclassing work here
gCtrlWndProc = SetWindowLongPtr(Button1.GetHwnd, GWL_WNDPROC, _
AddressOf CtrlProc)Using SetWindowLongPtr, the subclass procedure is removed from the control, similar to removing the subclass procedure from any other window:
Private Sub Form_Unload(Cancel As Integer)
Dim RetVal As Long
RetVal = SetWindowLongPtr(Button1.GetHwnd, GWL_WNDPROC, gCtrlWndProc)
End SubNow that we can subclass a control, we will move on to subclassing a control that we create within the VB environment.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access