一个exe工程 ,有一个form窗体,叫form1,form1上有个按钮command1,还有一个picture box,目的是当作其他窗体的容器;新增加一个active dll工程,在这个工程中新增一个窗体form2。我想通过单击form1上的command1,来把form2加载到form1中的picture box中,应该怎么做呢?

解决方案 »

  1.   

    主要利用SetParent 实现: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 Form_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 Form_Unload(Cancel As Integer)
        'Unload notepad
        DestroyWindow mWnd
        'End this program
        TerminateProcess GetCurrentProcess, 0
    End Sub
      

  2.   

    利用SetParent 
    Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long比如要把form2放到form1的picture1中(form1已经显示):
    load form2
    SetParent form2.hwnd,form1.picture1.hwnd即可
      

  3.   

    to: cso
    我想你误会我的意思了,我不是把外部程序比如notepad.exe放入我的form1中的picturebox容器中,而是要把一个active dll工程的一个form放入到exe工程中的form1的picturebox中!
      

  4.   

    那么没有什么关系的,只要你获得active dll工程的form句柄,就可以了,至于句柄如何获得,如果是别人的dll.你可以等到他的form出来后马上获得并且隐藏,至于是你的那就更好办了,直接提供一个接口就可以.
    反正你要把一个form放入到exe工程中的form1的picturebox中,是肯定会用到SetParent 的
      

  5.   

    我是这样做的:  在active dll工程中获得exe中的form1句柄,然后使用dll工程中的showForm方法来把form2放入到form1中的picture box中,但是最大的问题是我竟然不能编辑form2中的文本框,就是不能输入东西!!! 为什么啊,我怎么才能在exe中获得dll中的form2句柄? dll中实现这样的一个方法不行:
    Public Sub showReg(frm As Form)
     'codes
    End Sub错误是:private object modules cannot be used in public object modules as parameters or return types for public procedures, ...