Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Pro VB 6
|
Pro VB 6 For advanced Visual Basic coders working in version 6 (not .NET). Beginning-level questions will be redirected to other forums, including Beginning VB 6.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 25th, 2007, 11:41 AM
Authorized User
 
Join Date: Jul 2007
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old July 26th, 2007, 02:33 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

My guess would be that, for an app to respond as you want, it must be written to have the property that you are trying to set. Perhaps all windows have—by default—a receptacle of sorts to receive the SetParent message, but they won't respond to it unless the receptacle is more than a stub. (Sheer guess.)
 
Old July 27th, 2007, 10:39 AM
Authorized User
 
Join Date: Jul 2007
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Brian,

Thank you for your thoughts.... I am afraid of that too.... and that all software apps created by MS would be able to receive the SetParent message, and others would not.

Khoi Nguyen.

Quote:
quote:Originally posted by BrianWren
 My guess would be that, for an app to respond as you want, it must be written to have the property that you are trying to set. Perhaps all windows have—by default—a receptacle of sorts to receive the SetParent message, but they won't respond to it unless the receptacle is more than a stub. (Sheer guess.)
Khoi Nguyen
 
Old July 27th, 2007, 12:53 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Well, my take is that Microsoft would enforce this, and others might or might not implement it. I think it is probably overly black-and-white to say "and others would not"—too generalized...





Similar Threads
Thread Thread Starter Forum Replies Last Post
MDI & MDI Child Forms Menu's in VB2005 yulyos Visual Studio 2005 0 September 7th, 2007 09:49 AM
Controlling MDI child form from MDI parent panel LuxCoder Pro Visual Basic 2005 1 May 25th, 2007 12:20 PM
Controlling MDI child form from MDI parent panel LuxCoder VB.NET 2002/2003 Basics 7 April 11th, 2007 02:38 PM
Controlling MDI child form from MDI parent panel LuxCoder VB How-To 1 April 9th, 2007 03:25 PM
Controlling MDI child form from MDI parent panel LuxCoder Visual Basic 2005 Basics 2 April 9th, 2007 03:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.