The Salty Economist

Things I Should Have Learned in High School
posts - 56, comments - 0, trackbacks - 0

MDI Child Form in DLL VB6

OK, so we're still working with VB6. Here's how to put an MDI child form in a DLL, which cannot be done right of the box.

Credit for this goes to:  "https://groups.google.com/forum/#!topic/microsoft.public.vb.winapi/_AL4yJTiOwQ"

Step 1: Create an MDI Child forn in your main project file. The form is sparse, but does have some code:

----------------------------------------------------------------

Option Explicit

Public Declare Function SetParent& Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long)
Public Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long

Public Const WS_CHILD& = &H40000000
Public Const WS_CHILDWINDOW& = (WS_CHILD)
Public Const WS_EX_MDICHILD& = &H40&
Public Const GWL_EXSTYLE& = (-20)
Public Const GWL_STYLE& = (-16)
Public Const WS_VISIBLE& = &H10000000

Public Property Set DLLForm(RHS As Form)
   
    ' Set up our DLL Form
    Set m_frmDLL = RHS
   
    ' Stop drawing of the form
    LockWindowUpdate m_frmDLL.hWnd
   
    ' Set my parent as the container form
    SetParent m_frmDLL.hWnd, Me.hWnd
   
    ' Clear all styles
    SetWindowLong m_frmDLL.hWnd, GWL_STYLE, WS_VISIBLE
    SetWindowLong m_frmDLL.hWnd, GWL_EXSTYLE, 0&
   
    ' Move and redraw the form
    m_frmDLL.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight
    LockWindowUpdate 0&
    m_frmDLL.Refresh
 
End Property

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
   
    ' If closing the container form, make sure we reset everything back
    ' to how it was for VB
    m_frmDLL.Visible = False
    SetParent m_frmDLL.hWnd, 0&
    Unload m_frmDLL

End Sub

Private Sub Form_Resize()

    ' Resize our dll form
    If Not m_frmDLL Is Nothing Then
        m_frmDLL.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight
    End If

End Sub

Private Sub m_frmDLL_QueryUnload(Cancel As Integer, UnloadMode As Integer)
   
    ' If for some reason the dll form is going to be unloaded,
    ' unload the container form as well !
    Unload Me
 
End Sub
----------------------------------------------------------------

OK, so now we have a container form.  We can now create a dll that has any form and assign its container to be this MDI child.

Step 2 is to create a dll project.

Add your form (any form at all).   Let's call it: "myForm"  Note: do NOT set it to be an MDI child.  The main project and MDI Child container form will take care of that.

Add a class with a public interface.  Let's call it myClass.  Add a public method called LoadForm

------------------------------------------------------------------

Option Explict

Public Sub LoadForm(mdiChild As Object)

    Dim aForm As myForm
    Set aForm = New myFrom
    Set mdiChild.DLLForm = aForm  'This is what makes it work

    aForm.LoadMe 'Load up the form with data...

End Sub

------------------------------------------------------------------

All this does is instantiates your form and sets its container to be the MDI child that is passed in.  You can them load your form and do anything else you can with any other form.

 

 

Print | posted on Monday, April 28, 2014 9:09 AM |

Powered by:
Powered By Subtext Powered By ASP.NET