admin 管理员组

文章数量: 1086019


2023年12月19日发(作者:destination词根)

VB控制其他程序成为自己的子窗体

功能:VB控制其他程序成为自己的子窗体

VB界面如下:

图二

相关按钮的VB代码如下:

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 SetParent Lib "user32" (ByVal

hWndChild As Long, _

ByVal hWndNewParent As Long) As Long

Private OldParent&

Private Hwnd1&

Private Sub Form_Load()

=

=

End Sub

Private Sub Dir1_Change()

=

End Sub

Private Sub Drive1_Change()

=

End Sub

Private Sub Command1_Click()

'调用程序为本程序子窗口

Dim myexe As Long

Dim newhwnd As Long

Dim newexe As Long

Dim myvalue As Long

myexe = Shell( & "" & me,

vbNormalFocus) '调用程序

If myexe = 0 Then

MsgBox ""

Exit Sub

End If

newhwnd = FindWindow(ByVal 0&, ByVal 0&)

'获取windows句柄

Do While newhwnd <> 0

If GetParent(newhwnd) = 0 Then

myvalue = GetWindowThreadProcessId(newhwnd,

newexe) '获取窗口的进程

If newexe = myexe Then

Hwnd1& = newhwnd

Exit Do

End If

End If

newhwnd = GetWindow(newhwnd, GW_HWNDNEXT)

'获取窗口值

Loop

OldParent& = SetParent(Hwnd1&, )

'指定本程序为打开程序的父窗口

End Sub

Private Sub Command2_Click() '释放程序

SetParent Hwnd1&, OldParent&

End Sub

Private Sub Command3_Click()

End

End Sub

1.电脑上必须要先Microsoft Visual Studio(也就是VB软件)

2.用VB软件设置窗口如上(图二)

3.准备一个.EXE可执行文件或小程序(例子是打开一个相册)

运行结果:

完美实现对应功能。


本文标签: 程序 获取 打开 成为 控制