home *** CD-ROM | disk | FTP | other *** search
- Attribute VB_Name = "MFindPartLoop"
- ' *********************************************************************
- ' Copyright ⌐1995-97 Karl E. Peterson, All Rights Reserved
- ' *********************************************************************
- ' You are free to use this code within your own applications, but you
- ' are expressly forbidden from selling or otherwise distributing this
- ' source code without prior written consent.
- ' *********************************************************************
- Option Explicit
-
- #If Win16 Then
- DefInt A-Z
- ' Required Win16 API declarations
- Private Declare Function FindWindow Lib "User" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Integer
- Private Declare Function SetActiveWindow Lib "User" (ByVal hWnd As Integer) As Integer
- Private Declare Function ShowWindow Lib "User" (ByVal hWnd As Integer, ByVal nCmdShow As Integer) As Integer
- Private Declare Function GetWindow Lib "User" (ByVal hWnd As Integer, ByVal wCmd As Integer) As Integer
- Private Declare Function GetWindowText Lib "User" (ByVal hWnd As Integer, ByVal lpString As String, ByVal aint As Integer) As Integer
- Private Declare Function GetParent Lib "User" (ByVal hWnd As Integer) As Integer
- Private Declare Function IsIconic Lib "User" (ByVal hWnd As Integer) As Integer
- #ElseIf Win32 Then
- DefLng A-Z
- ' Required Win32 API declarations
- Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
- Private Declare Function SetActiveWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hWnd As Long) As Long
- Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
- 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 GetParent Lib "user32" (ByVal hWnd As Long) As Long
- Private Declare Function IsIconic Lib "user32" (ByVal hWnd As Long) As Long
- #End If
-
- ' Constants used with APIs
- Private Const SW_RESTORE = 9
-
- ' Constant used by GetWindowWord to find next window
- Private Const GW_HWNDNEXT = 2
-
- ' Constants used by FindWindowPartial
- Public Const FWP_STARTSWITH = 0
- Public Const FWP_CONTAINS = 1
-
- Public Sub AppActivatePartial(TitleContains$, Method%)
- Dim hWndApp 'As SysInt
- Dim nRet As Long
- '
- ' Retrieve window handle for first top-level window
- ' that starts with or contains the passed string.
- '
- hWndApp = FindWindowPartial(TitleContains, Method)
- If hWndApp Then
- '
- ' Switch to it, restoring if need be.
- '
- If IsIconic(hWndApp) Then
- Call ShowWindow(hWndApp, SW_RESTORE)
- End If
- nRet = SetActiveWindow(hWndApp)
- Else
- '
- ' Alert user that request failed.
- '
- MsgBox "No matching applications found."
- End If
- End Sub
-
- Public Function FindWindowPartial(TitleStart$, Method%) As Long
- Dim hWndTmp 'As SysInt
- Dim nRet 'As SysInt
- Dim TitleTmp As String
- '
- ' Find first window and loop through all subsequent
- ' windows in master window list.
- '
- hWndTmp = FindWindow(vbNullString, vbNullString)
- Do Until hWndTmp = 0
- '
- ' Make sure this window has no parent.
- '
- If GetParent(hWndTmp) = 0 Then
- '
- ' Retrieve caption text from current window.
- '
- TitleTmp = Space(256)
- nRet = GetWindowText(hWndTmp, TitleTmp, Len(TitleTmp))
- If nRet Then
- '
- ' Clean up return string, preparing for
- ' case-insensitive comparison.
- '
- TitleTmp = UCase(Left(TitleTmp, nRet))
- '
- ' Use appropriate method to determine if
- ' current window's caption either starts
- ' with or contains passed string.
- '
- Select Case Method
- Case FWP_STARTSWITH
- If InStr(TitleTmp, UCase(TitleStart)) = 1 Then
- FindWindowPartial = hWndTmp
- Exit Do
- End If
- Case FWP_CONTAINS
- If InStr(TitleTmp, UCase(TitleStart)) Then
- FindWindowPartial = hWndTmp
- Exit Do
- End If
- End Select
- End If
- End If
- '
- ' Get next window in master window list and continue.
- '
- hWndTmp = GetWindow(hWndTmp, GW_HWNDNEXT)
- Loop
- End Function
-