подскажите пожалуйста код, находящий hwnd чужого окна по фрагменту имени. как образец есть код, закрывающий окно по фраменту имени, но не мойму, где в коде нахождение hwnd
форма
Private Sub Command1_Click()
KillProga Text1.Text
End Sub
модуль
Option Explicit
Public TargetName As String
Public TargetHwnd As Long
Public Const GWL_STYLE = -16
Public Const WS_DISABLED = &H8000000
Public Const WS_CANCELMODE = &H1F
Public Const WM_CLOSE = &H10
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc 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 IsWindow Lib "user32" (ByVal hWnd As Long) _
As Long
Declare Function GetWindowLong Lib "user32" Alias _
"GetWindowLongA" (ByVal hWnd As Long, _
ByVal nIndex As Long) As Long
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
Sub KillProga(proga As String)
Do
TargetName = proga
TargetHwnd = 0
' Examine the window names.
EnumWindows AddressOf WindowEnumerator, 0
' See if we got an hwnd.
If TargetHwnd = 0 Then
Exit Do
Else
EndTask (TargetHwnd)
End If
Loop
End Sub
' Return False to stop the enumeration.
Public Function WindowEnumerator(ByVal app_hwnd As Long, _
ByVal lParam As Long) As Long
Dim buf As String * 256
Dim Title As String
Dim length As Long
' Get the window's title.
length = GetWindowText(app_hwnd, buf, Len(buf))
Title = Left$(buf, length)
' See if the title contains the target.
If InStr(Title, TargetName) > 0 Then
' Save the hwnd and end the enumeration.
TargetHwnd = app_hwnd
WindowEnumerator = False
Else
' Continue the enumeration.
WindowEnumerator = True
End If
End Function
Function EndTask(TargetHwnd As Long) As Long
Dim rc As Integer
Dim ReturnVal As Integer
' If TargetHwnd = Form1.hwnd Or GetWindow(TargetHwnd, _
' GW_OWNER) = Form1.hwnd Then
' End
' End If
If IsWindow(TargetHwnd) = False Then
GoTo EndTaskFail
End If
If (GetWindowLong(TargetHwnd, GWL_STYLE) _
And WS_DISABLED) Then
GoTo EndTaskSucceed
End If
'Close the window
If IsWindow(TargetHwnd) Then
If Not (GetWindowLong(TargetHwnd, GWL_STYLE) _
And WS_DISABLED) Then
'rc = PostMessage(TargetHwnd, WS_CANCELMODE, 0, 0&)
'rc = PostMessage(TargetHwnd, WM_CLOSE, 0, 0&)
DoEvents
End If
End If
GoTo EndTaskSucceed
EndTaskFail:
ReturnVal = False
GoTo EndTaskEndSub
EndTaskSucceed:
ReturnVal = True
EndTaskEndSub:
EndTask = ReturnVal
End Function