Ok, Making progress... I hope you are patient with my total overwhelmed attempt to master .NET. Actually the main problem was that I had to sleep the thread after the process.start (the loop watching the window handle did nothing but lock the application indefinitely). Anyway, I can't help but think there must be a better way since the sleep command is flaky based on the speed of application launch. Setting it too high is a significant performance loss - and an apparent interface freeze, but setting it too low and the VFP app does not become hosted.
A related issue is: I would like the VFP app to be in the proper state before making it visible. Currently a lot of window activity is going on which is very ugly! I tried to set the ProcessStartInfo instance to hidden, but that did nothing.
Another problem I am having is on the resize: Can you tell me why I get scrollbars in the _MDI region when I resize the form? How do I remove them?
Last, but not least (at least for now): What is the best way of having the hosted VFP app shutdown when the .NET form is closing? Here I am killing the process, but I don't suspect that is the recommended way of doing this!
Public
Class Form1Declare Function ShowWindow Lib "user32" (ByVal hWnd As System.IntPtr, ByVal nCmdShow As Integer) As Boolean
Declare Function SetParent Lib "user32" (ByVal hWndChild As System.IntPtr, ByVal hWndNewParent As System.IntPtr) As System.IntPtr
Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As System.IntPtr) As Integer
Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Integer, ByVal hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean
Dim _Mdi As MdiClient
Dim _MdiClientHandle As Integer
Dim _Processes As Collection = New Collection()
Private Sub DocMgrToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DocMgrToolStripMenuItem.Click
Dim AppPath As String = "c:\MyVfpApp\"
Dim App2Run As String = "TheBestVfpAppEver.exe"
If Not _Processes.Contains(App2Run) Then
Dim loStart As New System.Diagnostics.ProcessStartInfo(AppPath + App2Run)
loStart.WorkingDirectory = AppPath
Dim oProcess As System.Diagnostics.Process = System.Diagnostics.Process.Start(loStart)
System.Threading.Thread.Sleep(5000)
_MdiClientHandle = oProcess.MainWindowHandle.ToInt32()
SetParent(_MdiClientHandle, _Mdi.Handle)
SetWindowPos(_MdiClientHandle, 0, 0, 0, _Mdi.Width, _Mdi.Height, &H20)
_Processes.Add(oProcess, App2Run)
Else
MsgBox(App2Run + " is already open.")
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
_Mdi = New MdiClient()
_Mdi.BackColor = System.Drawing.Color.White
Me.IsMdiContainer = True
Me.Controls.Add(_Mdi)
End Sub
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
For Each oProcess As System.Diagnostics.Process In _Processes
If Not oProcess.HasExited Then
oProcess.Kill()
End If
Next
End Sub
Private Sub Form1_SizeChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
If _MdiClientHandle > 0 Then
SetWindowPos(_MdiClientHandle, 0, 0, 0, _Mdi.Width, _Mdi.Height, &H20)
End If
End Sub
End Class
Also: Why does my code sample insist on adding blank lines between each pasted line of code?