Community discussion forum

MDI Applications

  • 4 years ago

    I am using VB.net and would like to display a different application on the VB form as a child form. Is this possible?

  • 4 years ago

    In VB 6.0 is rather simple:  Just set the parent window.  The following code embeds the Windows calculator inside a regular VB form.

    Code:
    Option Explicit


    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SetParent Lib "user32.dll" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long


    Private hCalc As Long
    Private hParent As Long


    Private Sub Form_Load()
       hCalc = FindWindow(vbNullString, "Calculator")
       If hCalc Then
           hParent = SetParent(hCalc, Me.hWnd)
       End If
    End Sub


    Private Sub Form_Unload(Cancel As Integer)
       If hCalc Then
           SetParent hCalc, hParent
       End If
    End Sub


    To test this, start a standard project, then copy the above code into the form's module, open the calculator, and finally run the project.

  • 4 years ago
    Thank you for the help, but I am still lost. I copied your code into my form, but this does not seem to work. The form pops up without the calculator. I set the 'Me.IsMdiContainer = True' and copied your code as follows:

    Option Explicit On
    Public Class Form1
       Inherits System.Windows.Forms.Form

       Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
       Private Declare Function SetParent Lib "user32.dll" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

       Private hCalc As Long
       Private hParent As Long

       Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
           hCalc = FindWindow(vbNullString, "Calculator")
           If hCalc Then
               hParent = SetParent(hCalc, Me.Handle.ToInt32)
           End If
       End Sub

       Private Sub Form_Unload(ByVal Cancel As Integer)
           If hCalc Then
               SetParent(hCalc, hParent)
           End If
       End Sub
    End Class
  • 4 years ago

    Quote:
    To test this, start a standard project, then copy the above code into the form's module, open the calculator, and finally run the project.

    It seems to me that you forgot to open the calculator prior to running the example.

  • 4 years ago

    I did run the calculator prior to running the application, but it still does not seem to work.

  • 4 years ago
    I see your code and makes sense to me, but since I haven't programmed ever in .NET, I cannot be sure it is OK.  I assure you the code I presented worked OK for me under Windows XP.
  • 4 years ago
    ok, VB.NET is practically a whole new language in my opinion. that code seems very nice for VB6, but it definitely won't work for .NET. let me try to take a look for a VB.NET way...
  • 4 years ago

    After some sweat I found out how it is done in .net:


    Private hCalc As IntPtr
       Private hParent As IntPtr
       Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, _
           ByVal lpWindowName As String) As IntPtr
       Private Declare Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, _
           ByVal hWndNewParent As IntPtr) As IntPtr


       Private Sub frmMain_Load(ByVal sender As Object, _
           ByVal e As System.EventArgs) Handles MyBase.Load
           hCalc = FindWindow(vbNullString, "Calculator")
           If hCalc.ToInt32 <> 0 Then
               hParent = SetParent(hCalc, Me.Handle)
           End If
       End Sub


       Private Sub frmMain_Closed(ByVal sender As Object, _
           ByVal e As System.EventArgs) Handles MyBase.Closed
           If hCalc.ToInt32 <> 0 Then
               SetParent(hCalc, hParent)
           End If
       End Sub

  • 4 years ago

    ok in .NET you can load other forms from other apps into an mdi parent by using the assembly.LoadFrom method, a good article is here


    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet10082002.asp


    its pretty neat to think you can buiild new forms later on after initial development.

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback