Create a new module and paste this source code into it. You should name this class module basWindowUtils.If you have any questions, email us at help@codeoftheweek.com
'---------------------------------------------------------------------- ' ' Module Name: basWindowUtils ' Written By: C&D Programming Corp. ' Create Date: 8/99 ' Copyright: Copyright 1999 by C&D Programming Corp. Source ' code may not be reproduced except for use in a ' compiled executable. All rights reserved. If ' you would like to reprint any or all of this ' code please email us at info@codeoftheweek.com '---------------------------------------------------------------------- Option Explicit Option Compare Text ' for this module we only want to use case insensitive searches. ' if your application calls for case specific searches, comment ' the above line. 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 GetWindowTextLength Lib "user32" _ Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long Private Declare Function GetDesktopWindow Lib "user32" () As Long Private Declare Function GetWindow Lib "user32" _ (ByVal hWnd As Long, ByVal wCmd As Long) As Long Public Function WindowCaption(ByVal hWnd As Long) As String Dim lCaptionLength As Integer Dim sCaption As String ' figure out the length of the caption to retrieve. ' add 1 to allow for terminating null character in the ' GetWindowText API call lCaptionLength = GetWindowTextLength(hWnd) + 1 If lCaptionLength <= 0 Then Exit Function ' build a padded string to get the window caption sCaption = String$(lCaptionLength, vbNullChar) lCaptionLength = GetWindowText(hWnd, sCaption, lCaptionLength) ' return the caption we found WindowCaption = Left(sCaption, lCaptionLength) End Function ' ' You can use pattern matching to locate a window with a particular ' title. The standard pattern matching options are: ' ' ? Any single character. ' * Zero or more characters. ' # Any single digit (0–9). ' [charlist] Any single character in charlist. ' [!charlist] Any single character not in charlist. ' ' Additional details can be found in the Visual Basic Help File ' under the topic Like Operator ' Public Function FindAnyTopWindow(sCaption As String) As Long Dim hWnd As Long Const GW_CHILD = 5 Const GW_HWNDNEXT = 2 FindAnyTopWindow = 0 ' a 0 return value means the window was ' not found. If sCaption = "" Then Exit Function ' must pass in a valid caption ' get the first child of the desktop (all top level windows are ' "children" of the desktop). hWnd = GetWindow(GetDesktopWindow(), GW_CHILD) Do While hWnd <> 0 ' Check window caption If WindowCaption(hWnd) Like sCaption Then FindAnyTopWindow = hWnd Exit Function End If ' Get next window in chain hWnd = GetWindow(hWnd, GW_HWNDNEXT) Loop End Function