Option Explicit
'Sub Forms Module Copyright 1998 Norrisoft
'
'This module exports a Public Sub OpenSubForm
'The sub takes two parameters : a form, and an hWnd property
'The form is loaded, it is setup as a child window, and then displayed
'in the hWnd window.
'
'Any object that has an hWnd may be used as the host for the form.
'This includes picture boxes, buttons, etc, etc .....
'
'NB: Within the host form you should unload any subforms loaded in the
'Form_Unload event !
'
'Now you can write wizards ! ....... happy coding.
'
'Norris68
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex 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 SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Sub SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
Private Const GWL_STYLE = (-16)
Private Const GWL_HWNDPARENT = (-8)
Private Const WS_CHILD = &H40000000
Private Const HWND_TOP = 0&
Private Const SWP_NOZORDER = &H4
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_SHOWWINDOW = &H40
Public Sub OpenSubForm(SubForm As Form, HostHWND As Long)
Dim DispRec As RECT
Dim Style As Long
GetClientRect HostHWND, DispRec ' Get Display Rectangle dimensions
Load SubForm ' Load subform
Style = GetWindowLong(SubForm.hwnd, GWL_STYLE) ' Get current window style
Style = Style Or WS_CHILD ' Append "WS_CHILD" style to the hWnd window style
SetWindowLong SubForm.hwnd, GWL_STYLE, Style ' Add new style to window
SetParent SubForm.hwnd, HostHWND ' Set host window as parent window
SetWindowLong SubForm.hwnd, GWL_HWNDPARENT, HostHWND ' Save the hWnd Parent in hWnd's window struct.
' Display the subform
SetWindowPos SubForm.hwnd, _
HWND_TOP, 0&, 0&, DispRec.Right, DispRec.Bottom, _
SWP_NOZORDER Or SWP_NOACTIVATE Or SWP_SHOWWINDOW
End Sub