Results 1 to 7 of 7

Thread: How to get the Window handle by the window's caption

  1. #1
    Guest
    How to get the handle of a window by its caption?
    Please reply at the earliest.



    thanks

    R.Sridhar
    [email protected]

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Use the FindWindow function
    Code:
    Private Declare Function FindWindow _
     Lib "user32" Alias "FindWindowA" ( _
     ByVal lpClassName As String, _
     ByVal lpWindowName As String) As Long
    Pass the caption as the second argument.

  3. #3
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    actually, you would need the class name as well, for that to function

    try this example:
    this code in a module:
    Code:
    Option Explicit
    Public Declare Function GetDesktopWindow Lib "user32" () As Long
    
    Public Declare Function GetWindow Lib "user32" _
       (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    
    Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
       (ByVal hwnd As Long, ByVal lpString As String, _
        ByVal cch As Long) As Long
    
    Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
       (ByVal hwnd As Long, ByVal lpClassName As String, _
        ByVal nMaxCount As Long) As Long
    
    Public Const GW_HWNDFIRST = 0
    Public Const GW_HWNDLAST = 1
    Public Const GW_HWNDNEXT = 2
    Public Const GW_HWNDPREV = 3
    Public Const GW_OWNER = 4
    Public Const GW_CHILD = 5
    add a listbox and this in a form:
    Code:
    Private Sub Form_Load()
      
       'Used to return window handles.
        Dim TitleToFind As String, ClassToFind As String
        Dim r As Long
      
        List1.Clear
        
       'Set the FindWindowLike text values from the strings entered into the textboxes
        TitleToFind = "Calculator"
        ClassToFind = "*"
      
        Call FindWindowLike(0, TitleToFind, ClassToFind)
        lbCount = CStr(List1.ListCount) & " matches found"
      
    End Sub
    Private Function FindWindowLike(ByVal hWndStart As Long, WindowText As String, Classname As String) As Long
    
        Dim hwnd As Long
        Dim sWindowText As String
        Dim sClassname As String
        Dim r As Long
      
       'Hold the level of recursion and
       'hold the number of matching windows
        Static level As Integer
      
       'Initialize if necessary. This is only executed when level = 0
       'and hWndStart = 0, normally only on the first call to the routine.
        If level = 0 Then
           If hWndStart = 0 Then hWndStart = GetDesktopWindow()
        End If
      
       'Increase recursion counter
        level = level + 1
      
       'Get first child window
        hwnd = GetWindow(hWndStart, GW_CHILD)
    
        Do Until hwnd = 0
          
           'Search children by recursion
            Call FindWindowLike(hwnd, WindowText, Classname)
          
           'Get the window text and class name
            sWindowText = Space$(255)
            r = GetWindowText(hwnd, sWindowText, 255)
            sWindowText = Left(sWindowText, r)
            
            sClassname = Space$(255)
            r = GetClassName(hwnd, sClassname, 255)
            sClassname = Left(sClassname, r)
                  
           'Check if window found matches the search parameters
            If (sWindowText Like WindowText) And (sClassname Like Classname) Then
            
                List1.AddItem hwnd & vbTab & sClassname & vbTab & sWindowText
                FindWindowLike = hwnd
                         
               'uncommenting the next line causes the routine to
               'only return the first matching window.
               ' Exit Do
               
            End If
        
         'Get next child window
          hwnd = GetWindow(hwnd, GW_HWNDNEXT)
      
        Loop
     
     'Reduce the recursion counter
      level = level - 1
    
    End Function
    it checks for all the windows which have a caption like that!

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Originally posted by da_silvy
    actually, you would need the class name as well, for that to function
    Nope you don't just call FindWindow in the following manner
    Code:
    Dim hWnd As Long
    hWnd = FindWindow(vbNullString, "TheWindowCaption")
    Good luck!

  5. #5
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    alright, you're right again joacim, but my way lets you search for an incomplete title.

    for example:

    A would return a altavista browser
    and a folder open called antelope


  6. #6
    Guest
    Actually, da_silvy, you can accomplish what you just did which much less code using EnumWindows.

    Code for a Module
    Code:
    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)
          'Replace Calculator with with whatever title you want to find.
          'You can use wildcards to help narrow your search down.
          If sName Like "Calc*" Then MsgBox "Window is open"
        End If
        
        EnumWindowsProc = 1
    End Function
    Code for a Form with a CommandButton.
    Code:
    Private Sub Command1_Click()
        EnumWindows AddressOf EnumWindowsProc, 0
    End Sub

  7. #7
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    thanks megatron, that's a much better example

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