Allora ... eccoti un esempio di codice in cui, con un tasto lanci il notepad e con un altro lo chiudi
codice:
Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5
Private Const WM_CLOSE = &H10
Private Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, _
ByVal wCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, _
lpdwProcessId As Long) As Long
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Dim h As Long
Dim hw As Long
Private Sub Command1_Click()
h = Shell("notepad.exe", vbNormalFocus)
End Sub
Private Sub Command2_Click()
hw = GethWndFromProcessID(h)
SendMessage hw, WM_CLOSE, 0, 0
End Sub
Private Function GethWndFromProcessID(hProcessIDToFind As Long) As Long
Dim hDesktop As Long
Dim hChild As Long
Dim hChildProcessID As Long
hDesktop = GetDesktopWindow()
hChild = GetWindow(hDesktop, GW_CHILD)
Do While hChild
Call GetWindowThreadProcessId(hChild, hChildProcessID)
If hChildProcessID = hProcessIDToFind Then
GethWndFromProcessID = hChild
Exit Do
End If
hChild = GetWindow(hChild, GW_HWNDNEXT)
Loop
End Function