Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1460

Setup at runtime a form as a MDI child, export the MDI form as a normal form

$
0
0
This code shows how to set a MDI child forma as classical form
And vice-versa
Code:

' #VBIDEUtils#************************************************************
' * Author          :
' * Web Site        :
' * E-Mail          :
' * Date            : 11/01/2021
' * Time            : 12:45
' * Module Name      : Module1
' * Module Filename  : Module1.bas
' * Purpose          :
' * Purpose          :
' **********************************************************************
' * Comments        :
' *
' *
' * Example          :
' *
' * See Also        :
' *
' * History          :
' *
' *
' **********************************************************************

Option Explicit

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWndParent&, ByVal hWndChildAfter&, ByVal lpClassName$, ByVal lpWindowName$) 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 IsWindow Lib "user32.dll" (ByVal hWnd As Long) As Long
Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

Private Const GWL_STYLE = (-16)
Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_MINIMIZEBOX = &H20000

Private Sub DisableEnableMinMax(oForm As Form, bDisable As Boolean)
  ' #VBIDEUtils#***********************************************************
  ' * Author          :
  ' * Web Site        : http://www.syndicassist.com
  ' * E-Mail          : info@syndicassist.com
  ' * Date            : 06/26/2008
  ' * Time            : 18:05
  ' * Module Name      : Module1
  ' * Module Filename  : Module1.bas
  ' * Procedure Name  : DisableEnableMinMax
  ' * Purpose          :
  ' * Parameters      :
  ' *                    oForm As Form
  ' *                    bDisable As Boolean
  ' * Purpose          :
  ' **********************************************************************
  ' * Comments        :
  ' *
  ' *
  ' * Example          :
  ' *
  ' * See Also        :
  ' *
  ' * History          :
  ' *
  ' *
  ' **********************************************************************

  ' #VBIDEUtilsERROR#
  On Error GoTo ERROR_DisableEnableMinMax

  Dim nRet            As Long

  ' *** The following code strips out the minimize and maximize buttons from the main MDI Frame.
  ' *** There is no other way to do this that I can find.  Note that if you want to set the form caption dynamically, you must do this first, because
  ' *** the caption sets the window style when set dynamically, and undoes all the work.

  ' *** Get current window style value
  nRet = GetWindowLong(oForm.hWnd, GWL_STYLE)
  If bDisable Then
      ' *** Remove style settings for the buttons
      nRet = nRet Xor WS_MINIMIZEBOX
      nRet = nRet Xor WS_MAXIMIZEBOX
  Else
      nRet = nRet Or WS_MINIMIZEBOX
      nRet = nRet Or WS_MAXIMIZEBOX
  End If
 
  ' *** Put the style back
  nRet = SetWindowLong(oForm.hWnd, GWL_STYLE, nRet)

EXIT_DisableEnableMinMax:
  On Error Resume Next

  Exit Sub

  ' #VBIDEUtilsERROR#
ERROR_DisableEnableMinMax:
  Resume EXIT_DisableEnableMinMax

End Sub

Private Function LoadChildForm(frmChild As VB.Form, hWndParent As Long, Optional bDesktop As Boolean = False) As Long
  ' #VBIDEUtils#************************************************************
  ' * Author          :
  ' * Web Site        :
  ' * E-Mail          :
  ' * Date            : 11/01/2021
  ' * Time            : 12:45
  ' * Module Name      : Module1
  ' * Module Filename  : Module1.bas
  ' * Procedure Name  : LoadChildForm
  ' * Purpose          :
  ' * Parameters      :
  ' *                    frmChild As VB.Form
  ' *                    hWndParent As Long
  ' *                    Optional bDesktop As Boolean = False
  ' * Purpose          :
  ' **********************************************************************
  ' * Comments        :
  ' *
  ' *
  ' * Example          :
  ' *
  ' * See Also        :
  ' *
  ' * History          :
  ' *
  ' *
  ' **********************************************************************

  ' #VBIDEUtilsERROR#
  On Error GoTo ERROR_LoadChildForm

  If Not bDesktop And (hWndParent = 0) Then
      Err.Raise vbObjectError + 1, "LoadChildForm", "bDesktop flag must be True if you wish to set " & "this form to be a child of desktop."
      Exit Function
  End If

  If (Not CBool(IsWindow(hWndParent))) And (hWndParent <> 0) Then
      Err.Raise vbObjectError + 1, "LoadChildForm", "Invalid parent window handle."
      Exit Function
  End If

  Load frmChild
  LoadChildForm = SetParent(frmChild.hWnd, hWndParent)

EXIT_LoadChildForm:
  On Error Resume Next

  Exit Function

  ' #VBIDEUtilsERROR#
ERROR_LoadChildForm:
  Resume EXIT_LoadChildForm

End Function

Public Sub MDI_Not_Child(frmChild As Form)
  ' #VBIDEUtils#************************************************************
  ' * Author          :
  ' * Web Site        :
  ' * E-Mail          :
  ' * Date            : 10/26/2019
  ' * Time            : 11:06
  ' * Module Name      : Module1
  ' * Module Filename  : Module1.bas
  ' * Procedure Name  : MDI_Not_Child
  ' * Purpose          :
  ' * Parameters      :
  ' *                    frmChild As Form
  ' * Purpose          :
  ' **********************************************************************
  ' * Comments        :
  ' *
  ' *
  ' * Example          :
  ' *
  ' * See Also        :
  ' *
  ' * History          :
  ' *
  ' *
  ' **********************************************************************

  ' #VBIDEUtilsERROR#
  On Error GoTo ERROR_MDI_Not_Child

  Call DisableEnableMinMax(frmChild, False)
  Call LoadChildForm(frmChild, 0, True)

EXIT_MDI_Not_Child:
  On Error Resume Next

  Exit Sub

  ' #VBIDEUtilsERROR#
ERROR_MDI_Not_Child:
  Resume EXIT_MDI_Not_Child

End Sub

Public Sub MDI_To_Child(frmParent As Form, frmChild As Form)
  ' #VBIDEUtils#************************************************************
  ' * Author          :
  ' * Web Site        :
  ' * E-Mail          :
  ' * Date            : 10/06/2019
  ' * Time            : 08:49
  ' * Module Name      : Module1
  ' * Module Filename  : Module1.bas
  ' * Procedure Name  : MDI_To_Child
  ' * Purpose          :
  ' * Parameters      :
  ' *                    frmParent As Form
  ' *                    frmChild As Form
  ' * Purpose          :
  ' **********************************************************************
  ' * Comments        :
  ' *
  ' *
  ' * Example          :
  ' *
  ' * See Also        :
  ' *
  ' * History          :
  ' *
  ' *
  ' **********************************************************************

  ' #VBIDEUtilsERROR#
  On Error GoTo ERROR_MDI_To_Child

  Dim hClientArea      As Long
  hClientArea = FindWindowEx(frmParent.hWnd, 0&, "MDIClient", vbNullChar)

  Call DisableEnableMinMax(frmChild, True)
  Call LoadChildForm(frmChild, hClientArea)

  frmChild.Move 0, 0

EXIT_MDI_To_Child:
  On Error Resume Next

  Exit Sub

  ' #VBIDEUtilsERROR#
ERROR_MDI_To_Child:
  Resume EXIT_MDI_To_Child

End Sub

Code:

Private Sub Command1_Click()

  If Command1.Tag = vbNullString Then
      Call MDI_Not_Child(Me)
      Command1.Tag = "MDI"
  Else
      Call MDI_To_Child(frmMain, Me)
      Command1.Tag = vbNullString
  End If

End Sub

Sample project attached
Attached Files

Viewing all articles
Browse latest Browse all 1460

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>