Option Explicit
Private 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 Long
Private Const GW_HWNDNEXT = 2
Private hwndApplicationParent As Long
Private hwndApplication As Long
Private Function Getwindowhwnd(ByVal lSourceId As Long) As Long
Dim hwndTemp As Long
Dim lProcessId As Long
Dim lIdTemp As Long
hwndTemp = FindWindow(ByVal 0&, ByVal 0&)
Do While hwndTemp <> 0
If GetParent(hwndTemp) = 0 Then
lIdTemp = GetWindowThreadProcessId(hwndTemp, lProcessId)
If lProcessId = lSourceId Then
Getwindowhwnd = hwndTemp
Exit Do
End If
End If
hwndTemp = GetWindow(hwndTemp, GW_HWNDNEXT)
Loop
End Function
Private Sub Form_Load()
dlgOpen.Filter = "应用程序|*.exe"
dlgOpen.Flags = cdlOFNFileMustExist Or cdlOFNLongNames '注意参数搭配
End Sub
Private Sub CmdOpen_Click()
Dim lId As Long
On Error Resume Next
dlgOpen.ShowOpen
If Trim(dlgOpen.FileName) = "" Then Exit Sub
lId = Shell(dlgOpen.FileName, vbMinimizedFocus)
If lId = 0 Then
MsgBox "应用程序不能正确执行 ", vbOKOnly + vbInformation
Exit Sub
End If
hwndApplication = Getwindowhwnd(lId)
hwndApplicationParent = SetParent(hwndApplication, Me.hwnd)
End Sub
温馨提示:答案为网友推荐,仅供参考