Results 1 to 3 of 3

Thread: FindWindow with multiple possibilities

  1. #1

    Thread Starter
    New Member Eeyore82's Avatar
    Join Date
    Apr 2001
    Location
    Gloucester, UK
    Posts
    13

    FindWindow with multiple possibilities

    I want to use the FindWindow API in conjunction with previnstance to find the handle of the previous instance of my app. However, the caption on my form is variable depending on what it is doing and there may be more than one window with the classname "ThunderRT6FormDC". So, my questions are:

    Is there any way to get FindWindow to return an array with the hwnd's to all applicable windows so as i can find the correct one from there (i.e. by checking its icon or something)?

    OR

    Can I search for just part of a form's caption? This way i could keep part of the caption (for example: the first four letters) constant and use FindWindow to search for that?

    Hope that makes some sense

    Thanx

  2. #2
    Megatron
    Guest
    Add to a Module
    VB Code:
    1. Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    2. Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    3. Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    4.  
    5. Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
    6.     Dim Length As Long
    7.     Dim sName As String
    8.    
    9.     Length = GetWindowTextLength(hwnd) + 1
    10.    
    11.     If Length > 1 Then
    12.       sName = Space(Length)
    13.       GetWindowText hwnd, sName, Length
    14.       sName = Left(sName, Length - 1)
    15.       'Use the Like operator to search with wildcards
    16.       If sName Like "*My Window Title*" And hWnd <> Form1.hWnd Then
    17.            MsgBox "The handle is: " & hwnd
    18.       End If
    19.     End If
    20.    
    21.     EnumWindowsProc = 1
    22. End Function

    Usage:
    VB Code:
    1. EnumWindows AddressOf EnumWindowsProc, 0

  3. #3

    Thread Starter
    New Member Eeyore82's Avatar
    Join Date
    Apr 2001
    Location
    Gloucester, UK
    Posts
    13
    Thanx Megatron, looks like just what i need

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width