Option Explicit
'========================================================
' API Declarations
'========================================================
Const GW_HWNDFIRST& = 0
Const GW_HWNDNEXT& = 2
Const GW_OWNER& = 4
Private Declare Function GetWindow _
Lib "user32" ( _
ByVal hwnd As Long, _
ByVal wCmd As Long) As Long
Const WS_DISABLED& = &H8000000
Const GWL_STYLE& = -16
Private Declare Function GetWindowLong _
Lib "user32" Alias "GetWindowLongA" ( _
ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Const WS_CANCELMODE& = &H1F
Const WM_CLOSE& = &H10
Private Declare Function PostMessage _
Lib "user32" Alias "PostMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam 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 GetWindowTextLength _
Lib "user32" Alias "GetWindowTextLengthA" ( _
ByVal hwnd As Long) As Long
Private Declare Function IsWindow _
Lib "user32" (ByVal hwnd As Long) As Long
'========================================================
' Program Procedures
'========================================================
Sub FindandCloseApp(ByVal partialCaption$)
Dim Whnd&, L&, Nam$
partialCaption = LCase$(partialCaption)
Whnd = GetWindow(Form1.hwnd, GW_HWNDFIRST)
Do While Whnd <> 0
If IsWindow(Whnd) Then
L = GetWindowTextLength(Whnd)
If L > 0 Then
Nam = Space$(L + 1)
L = GetWindowText(Whnd, Nam, L + 1)
Nam = LCase$(Left$(Nam, L))
If InStr(Nam, partialCaption) Then
EndTask Whnd
Exit Do
End If
End If
End If
Whnd = GetWindow(Whnd, GW_HWNDNEXT)
DoEvents
Loop
End Sub
Private Sub EndTask(Whnd As Long)
'Do not close this window
If Whnd = Form1.hwnd Then Exit Sub
'Do not close this window's child
' (Do it without API)
If GetWindow(Whnd, GW_OWNER) _
= Form1.hwnd Then Exit Sub
'Do not close if disabled
If (GetWindowLong(Whnd, GWL_STYLE) _
And WS_DISABLED) Then Exit Sub
PostMessage Whnd, WS_CANCELMODE, 0&, 0&
PostMessage Whnd, WM_CLOSE, 0&, 0&
End Sub
Private Sub Command1_Click()
FindandCloseApp Text1.Text
End Sub
Private Sub Form_Activate()
Shell "Notepad.exe"
Text1 = "Note"
End Sub
|