VB 让外部程序在VB程序的窗体里面运行

前端之家收集整理的这篇文章主要介绍了VB 让外部程序在VB程序的窗体里面运行前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_403_1@Option Explicit

@H_403_1@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

@H_403_1@Private Const GW_HWNDNEXT = 2
Private m_Hwnd As Long

@H_403_1@Private Sub Form_Load()

@H_403_1@ Dim dblPid As Long

@H_403_1@ Call LockWindowUpdate(GetDesktopWindow)

@H_403_1@ dblPid = Shell("c:\windows\notepad.exe",vbNormalFocus)

@H_403_1@ m_Hwnd = InstanceToWnd(dblPid) '根据进程PID找窗口句柄

@H_403_1@ SetParent m_Hwnd,Me.hwnd

@H_403_1@ Putfocus m_Hwnd '记事本设置焦点

@H_403_1@ Call LockWindowUpdate(0)

@H_403_1@End Sub

@H_403_1@Function InstanceToWnd(ByVal target_pid As Long) As Long

@H_403_1@ Dim i As Long,lHwnd As Long,lPid As Long,lThreadId As Long

@H_403_1@ lHwnd = FindWindow(ByVal 0&,ByVal 0&) '查找第一个窗口

@H_403_1@ Do While lHwnd <> 0

@H_403_1@ i = i + 1

@H_403_1@ If i Mod 20 = 0 Then DoEvents

@H_403_1@ '判断窗口是否没父窗口
If GetParent(lHwnd) = 0 Then

@H_403_1@ '获取该窗口的线程ID
lThreadId = GetWindowThreadProcessId(lHwnd,lPid)

@H_403_1@ If lPid = target_pid Then '找到PID所在窗口句柄

@H_403_1@ InstanceToWnd = lHwnd
Exit Do

@H_403_1@ End If

@H_403_1@ End If

@H_403_1@ '继续查找下一个兄弟窗口
lHwnd = GetWindow(lHwnd,GW_HWNDNEXT)

@H_403_1@ Debug.Print Hex$(lHwnd)

@H_403_1@ Loop

@H_403_1@End Function

@H_403_1@Private Sub Form_Unload(Cancel As Integer)

@H_403_1@ Call DestroyWindow(m_Hwnd)
'TerminateProcess GetCurrentProcess,0 '野蛮了些
Set Form1 = Nothing

@H_403_1@End Sub

原文链接:https://www.f2er.com/vb/261109.html

猜你在找的VB相关文章