Option Explicit
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal MSG As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private mdiChildren As New Collection
Private Const WM_ACTIVATE = &H6
Private Const GWL_WNDPROC = (-4)
Private lngMasterWindowProc As Long
Private lngMasterWindowHWnd As Long
Public Function WindowProc_Child(ByVal hWnd As Long, ByVal MSG As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'Handle the messages that Windows sends
Select Case MSG
Case WM_ACTIVATE
'When Windows tells us the window is activated, notify the parent window of the activation
CallWindowProc lngMasterWindowProc, hWnd, MSG, wParam, lParam
End Select
'Call the standard Windows procedure of the child
WindowProc_Child = CallWindowProc(mdiChildren.Item("x" & hWnd), hWnd, MSG, wParam, lParam)
End Function
Public Sub InitializeSubclassing(MasterWindowHWnd As Long)
'Get the Windows Proc of the master window
lngMasterWindowProc = GetWindowLong(MasterWindowHWnd, GWL_WNDPROC)
'Store the HWND for our usage later
lngMasterWindowHWnd = MasterWindowHWnd
End Sub
Public Sub MakeMDIChild(ChildWindowHWnd As Long)
'Record the old windows proc for the MDI child.
mdiChildren.Add GetWindowLong(ChildWindowHWnd, GWL_WNDPROC), ("x" & ChildWindowHWnd)
'Begin subclassing the new child window
SetWindowLong ChildWindowHWnd, GWL_WNDPROC, AddressOf WindowProc_Child
'Set the parent of the child, making it a MDI child window
SetParent ChildWindowHWnd, lngMasterWindowHWnd
End Sub
Public Sub RemoveMDIChild(ChildWindowHWnd As Long)
'Reset the child window's windows proc to its default, removing our subclassing
SetWindowLong ChildWindowHWnd, GWL_WNDPROC, mdiChildren.Item(("x" & ChildWindowHWnd))
'Remove the record of the MDI child
mdiChildren.Remove ("x" & ChildWindowHWnd)
End Sub