PDA

Click to See Complete Forum and Search --> : Search for Window Handle


assbeef
Jun 17th, 2001, 10:36 AM
I need code to search for a piece of a window's handle, and if found, it will destroy that window. I have code that does this but you must supply the entire handle and case sensitive. Is there any way of only using part of the window's handle? An example would be to destroy IE, or Netscape or whatever multi-form application.

Thanks

Megatron
Jun 17th, 2001, 12:38 PM
Use the EnumWindows function.

'Add to a Module
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim Length As Long
Dim sName As String
Dim Temp As String
Static iCount As Integer

iCount = iCount + 1
Length = GetWindowTextLength(hwnd) + 1

If Length > 1 Then
sName = Space(Length)
GetWindowText hwnd, sName, Length
sName = Left(sName, Length - 1)
If sName Like "*My Title Fragment*" Then MsgBox "Window Found!"
End If

EnumWindowsProc = 1
End Function

'Usage:
EnumWindows AddressOf EnumWindowsProc, 0