高分求教,如何在MDI主窗口中使用shell(或shellexecute)调用另外一个程序(也是自己写的)运行在主窗口中(类似MDI Child)?同时如何传递参数,在主窗口中怎么写,在被调用窗口中怎么写?

解决方案 »

  1.   

    如何调用外部程序同MDI Child一样使用?
      

  2.   

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
    Const GW_HWNDNEXT = 2
    Dim mWnd As Long
    Function InstanceToWnd(ByVal target_pid As Long) As Long
        Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
        'Find the first window
        test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
        Do While test_hwnd <> 0
            'Check if the window isn't a child
            If GetParent(test_hwnd) = 0 Then
                'Get the window's thread
                test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
                If test_pid = target_pid Then
                    InstanceToWnd = test_hwnd
                    Exit Do
                End If
            End If
            'retrieve the next window
            test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
        Loop
    End Function
    Private Sub MDIForm_Load()
        'KPD-Team 1999
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        Dim Pid As Long
        'Lock the window update
        LockWindowUpdate GetDesktopWindow
        'Execute notepad.Exe
        Pid = Shell("c:\windows\notepad.exe", vbNormalFocus)
        If Pid = 0 Then MsgBox "Error starting the app"
        'retrieve the handle of the window
        mWnd = InstanceToWnd(Pid)
        'Set the notepad's parent
        SetParent mWnd, Me.hwnd
        'Put the focus on notepad
        Putfocus mWnd
        'Unlock windowupdate
        LockWindowUpdate False
    End Sub
    Private Sub MDIForm_Unload(Cancel As Integer)
        'Unload notepad
        DestroyWindow mWnd
        'End this program
        TerminateProcess GetCurrentProcess, 0
    End Sub
      

  3.   

    我发现如果我的外部程序中用模块的sub main作为启动,则无法提前显示该程序的窗口,所以失败,能否在sub main中就制定该窗口的句柄,或窗口的标题,或者让MDI在执行shell以后先等待sub main运行完,才开始执行?
      

  4.   

    使用SetParent函数应该就可以实现
      

  5.   

    //////////我发现如果我的外部程序中用模块的sub main作为启动,则无法提前显示该程序的窗口,所以失败,能否在sub main中就制定该窗口的句柄,或窗口的标题,或者让MDI在执行shell以后先等待sub main运行完,才开始执行?///////把窗体先SHOW出来再进行处理。
      

  6.   

    同时能显示两个vbmodal窗体吗?
    不能
      

  7.   


      Dim Rtn As Long
      
      Rtn = ShellExecute(Me.hwnd, "Open", _
                 "http://www.csdn.net", "", "www.csdn.net", 1)
      

  8.   

    俺给你一个思路吧!
    你的外部程序是自己写的吧!!
    那你可以将MDI的hWND作为参数传给你的外部程序,,然后在外部程序里面用API设置程序窗体的Parent不就可以啦!!!
      

  9.   


    下面是使用 SetParent 设置一个程序的新父窗口,你参考一下吧.Option ExplicitPrivate Const GW_HWNDNEXT = 2Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As LongPrivate old_parent As Long
    Private child_hwnd As LongPrivate Sub cmdRun_Click()
    Dim pid As Long
    Dim buf As String
    Dim buf_len As Long
    Dim styles As Long
        pid = Shell("你的程序", vbNormalFocus)
        If pid = 0 Then
            MsgBox "Error starting program"
            Exit Sub
        End If
        child_hwnd = InstanceToWnd(pid)
        old_parent = SetParent(child_hwnd, MDIForm1.hwnd)
        cmdRun.Enabled = False
        cmdFree.Enabled = True
    End SubPrivate Function InstanceToWnd(ByVal target_pid As Long) As Long
    Dim test_hwnd As Long
    Dim test_pid As Long
    Dim test_thread_id As Long    test_hwnd = FindWindow(ByVal 0&, ByVal 0&)    Do While test_hwnd <> 0
            If GetParent(test_hwnd) = 0 Then
                test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)            If test_pid = target_pid Then
                    ' This is the target.
                    InstanceToWnd = test_hwnd
                    Exit Do
                End If
            End If        test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
        Loop
    End FunctionPrivate Sub cmdFree_Click()
        SetParent child_hwnd, old_parent    cmdRun.Enabled = True
        cmdFree.Enabled = False
    End Sub