Visual Basic System Services
GetWindow: Find Applications of a Specific Class or Window Title
Posted:   Monday May 19, 1997
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6
Developed with:   VB4-32, Windows 95
OS restrictions:   None
Author:   VBnet - Randy Birch

This code will list all active windows containing either the Class name of the application to search for, the Window title of the application to search for, or any combination of both. It uses a recursive search function to retrieve all windows, and compare the window titlebar caption against the value being sought.

Unlike the FindWindow API, which requires that you know the precise (and current) window name of the application you're trying to locate, the FindWindowLike routine allows even partial strings in searching out the window. This means that, for finding Microsoft Word, you could enter as the window caption "Word", "Microsoft", "Microsoft Word", "oft W", or even simply "M". Similarly, you could specify the Classname as "OpusApp" (MS Word 7's actual Window class name), "Opus", or an asterisk for any class name.

The only condition that the code makes is that, at a minimum, the Class name parameter be at least "*".

The original author of the FindWindowLike routine is unknown.

Start a new project, and on the form, add a command button (cmdStart), two textboxes (txtClass & txtTitle), a label for the number of items found (lbCount), labels for the text and list descriptions as shown in the illustration, and a listbox (List1).

 BAS Module Code
Place the following code into the general declarations area of a bas module:

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
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
 Form Code
To the form, add the following code:

Option Explicit

Private Sub cmdStart_Click()
  'Used to return window handles.
   Dim sTitleToFind As String, sClassToFind As String
  'Set the FindWindowLike text values from 
  'the strings entered into the textboxes
   sTitleToFind = "*" & (txtTitle) & "*"
   sClassToFind = (txtClass)
   Call FindWindowLike(0, sTitleToFind, sClassToFind)
   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 & _
         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)
  'Reduce the recursion counter
   level = level - 1

End Function
Save the project, then run. Enter a full or partial string into the Window title text box. Note that the search is case-sensitive as coded above. You can also enter any class name into the Class name textbox if you want too, or run using the default *.

Note as well that the textbox containing the text you've entered will also appear in the list because of the call to the GetWindowText API. You could code the routine to only list parent windows by discarding those who return a value to an IsChild() API call.


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter