想写个VB小程序,它的功能是用来打开另几个独立的程序(EXE文件),如果关掉这个VB小程序的话,由它所打开的程序都要自行关闭。条件:
1、能在2000/xp下通用(windows 9x下的方法以知道了)
2、如时刻意用"windows任务管理器"中止这个程序的进程时,它都能实现同时关闭其它经由它所打开的程序,有如"父子"关系
能提供编程方向或提供例的必给分先谢谢 ^_^
1、能在2000/xp下通用(windows 9x下的方法以知道了)
2、如时刻意用"windows任务管理器"中止这个程序的进程时,它都能实现同时关闭其它经由它所打开的程序,有如"父子"关系
能提供编程方向或提供例的必给分先谢谢 ^_^
Declare Function EnumWindows Lib "user32" (ByVal wndenmprc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const WM_CLOSE = &H10
Private Target As StringPublic Function closewin(ByVal app_hWnd As Long, ByVal param As Long) As Long
Dim buf As String * 256
Dim title As String
Dim length As Long length = GetWindowText(app_hWnd, buf, Len(buf))
title = Left$(buf, length) If InStr(title, Target) <> 0 Then
SendMessage app_hWnd, WM_CLOSE, 0, 0
End If
closewin = 1
End FunctionPublic Sub pausetask(app_name As String)
Target = app_name
EnumWindows AddressOf closewin, 0
End Sub'以下代码放入窗体Option ExplicitPrivate Sub Form_Load()
Dim i
i = Shell("notepad.exe", 1)
End SubPrivate Sub Form_Unload(Cancel As Integer)()
pausetask "记事本"
End Sub试试看,应该是你需要的结果吧!别忘了给分啊!兄弟好穷!
2、如时刻意用"windows任务管理器"中止这个程序的进程时,它都能实现同时关闭其它经由它所打开的程序,有如"父子"关系!!!
Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long' Return the window handle for an instance handle.
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 ' Get the first window handle.
test_hwnd = FindWindow(ByVal 0&, ByVal 0&) ' Loop until we find the target or we run out
' of windows.
Do While test_hwnd <> 0
' See if this window has a parent. If not,
' it is a top-level window.
If GetParent(test_hwnd) = 0 Then
' This is a top-level window. See if
' it has the target instance handle.
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 ' Examine the next window.
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End Function以下是一个菜单调用:
Private Sub mnuOpenCalc_Click()
Dim pid As Long pid = Shell("calc.exe", vbNormalFocus)
If pid = 0 Then
MsgBox "Error starting program"
Exit Sub
End If Calc_Hwnd& = InstanceToWnd(pid) SetParent(Calc_Hwnd&, Me.hwnd)
End Sub
SetParent Calc_Hwnd&, Me.hwnd去掉括号才对。