Results 1 to 3 of 3

Thread: Hueristic FindWindow()

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    12

    Hueristic FindWindow()

    Hiya again,

    I was just wondering if there was any way to do something like so

    Code:
    lMyHWND = FindWindow(vbNullString, "Asdf | *")
    Where * would be a wildcard, because it varies. And no, unfortunately I can't just use the class name because there are multiple windows with the same class name.

    Any help is much appreciated.

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Hueristic FindWindow()

    EDIT > MS has an example that allows using wildcards , How To Get a Window Handle Without Specifying an Exact Title

    Otherwise you can try something like this by passing a partial name of the programs title bar caption,
    Code:
    ' Form*.frm
    Option Explicit
    
    Private Sub Command1_Click()
        
        Dim i As Long
        
        ' search for window titles like,
        FindWindowLike "Internet Explorer"
        
        ' show results
        If UBound(WinMatch) = 0 Then
            Debug.Print "No Match Found"
        Else
            Debug.Print "Matches Found = " & UBound(WinMatch)
            Debug.Print "-"
            For i = 0 To UBound(WinMatch) - 1
                Debug.Print "Title = " & WinMatch(i).Title ' title caption
                Debug.Print "Hwnd = " & WinMatch(i).Hwnd ' handle
                Debug.Print "-"
            Next i
        End If
        
    End Sub
    Code:
    ' module*.bas
    Option Explicit
    Private Declare Function GetWindow Lib "user32" (ByVal Hwnd As Long, ByVal wCmd 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 GetForegroundWindow Lib "user32.dll" () As Long
    Private Const GW_HWNDNEXT = 2
    Private Const GW_CHILD = 5
    
    Private Type WindowInfo
        Hwnd As Long
        Title As String
    End Type
    
    Public WinMatch() As WindowInfo
    
    Public Function FindWindowLike(sPartOfCaption As String) As Long
    
        Dim sCurWinText As String
        Dim Hwnd As Long, ret As Long
        ReDim WinMatch(0)
        
        Hwnd = GetForegroundWindow
        Do Until Hwnd = 0
            sCurWinText = Space$(255)
            ret = GetWindowText(Hwnd, sCurWinText, 255)
            sCurWinText = Left$(sCurWinText, ret)
            If InStr(1, LCase(sCurWinText), LCase(sPartOfCaption)) <> 0 Then
                WinMatch(UBound(WinMatch)).Title = sCurWinText
                WinMatch(UBound(WinMatch)).Hwnd = Hwnd
                ReDim Preserve WinMatch(UBound(WinMatch) + 1)
            End If
            Hwnd = GetWindow(Hwnd, GW_HWNDNEXT)
        Loop
    
    End Function
    Last edited by Edgemeal; Sep 27th, 2008 at 08:52 PM.

  3. #3
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Hueristic FindWindow()

    RobDog88 also had a similar project. Might also help you.

    Find Window handle by Partial Caption
    http://www.vbforums.com/showthread.php?t=316924

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