Re-parenting a new app in an MDI environment
Hi all,
This is a second issue that I am currently faced with.
I am working with an MDI app at work. Within it, we need to launch another app, and make it appear that it's an MDI child, part of the MDI app as a whole.
I have been able to achieve this successfully, by using the Shell() function in VB6. Below is the line of code that does this:
lngProcessID = Shell(strCmd, vbNormalFocus)
where strCmd is the string of the path pointing to the other app. At the same time, I'm also getting the process ID returned from the Shell() function call.
To do the re-parenting, I had written code to grab a handle on the window of the other app's process, and then use SetParent() function to set its parent to be my MDI parent. Below is the actual code:
If lngProcessID = 0 Then
GoTo ReportsErr
Else
'Get the handle on the window just launched
lngProcessHandle = InstanceToWnd(lngProcessID)
'Re-parent the launched app so it lies inside the MDI frmControl
Call SetParent(lngProcessHandle, frmControl.hwnd)
End If
Here is the code for InstanceToWnd() function:
Public Function InstanceToWnd(ByVal lngTargetPID As Long) As Long
'
' Return the window handle for an instance handle.
'
Dim lngTestHWnd As Long
Dim lngTestPID As Long
Dim lngTestThreadID As Long
Dim strEnv As String
' Get the first window handle.
lngTestHWnd = FindWindow(ByVal 0&, ByVal 0&)
' Loop until we find the target or we run out
' of windows.
Do While lngTestHWnd <> 0
' See if this window has a parent. If not,
' it is a top-level window.
If GetParent(lngTestHWnd) = 0 Then
' This is a top-level window. See if
' it has the target instance handle.
lngTestThreadID = GetWindowThreadProcessId(lngTestHWnd, lngTestPID)
If lngTestPID = lngTargetPID Then
' This is the target.
InstanceToWnd = lngTestHWnd
Exit Do
End If
End If
' Examine the next window.
lngTestHWnd = GetWindow(lngTestHWnd, GW_HWNDNEXT)
Loop
End Function
I tried this on "notepad.exe" and I was able to set it to work. But when I tried it on a non-Microsoft app., it didn't work. The new non-MS app was launched as top-level and did not appear to be part of the MDI environment at all. I could move its window outside the boundary of my MDI app.
Does anyone have any ideas on why this does not work? I tested this with several other MS-apps (like MS word, excel, etc.) and they all worked fine. I'm a bit suspicious that SetParent does not work with a non-MS app. Is this true?
Thank you very much for all your help,
Khoi.
Khoi Nguyen
__________________
Khoi Nguyen
|