home *** CD-ROM | disk | FTP | other *** search
/ CD Shareware Magazine 1999 April / CD_Shareware_Magazine_31.iso / Free / Prg / FindPart.exe / FindPart5.bas < prev    next >
Encoding:
BASIC Source File  |  1998-03-02  |  4.5 KB  |  135 lines

  1. Attribute VB_Name = "FindPartEnum"
  2. ' *********************************************************************
  3. '  Copyright ⌐1995-98 Karl E. Peterson, All Rights Reserved
  4. ' *********************************************************************
  5. '  You are free to use this code within your own applications, but you
  6. '  are expressly forbidden from selling or otherwise distributing this
  7. '  source code without prior written consent.
  8. ' *********************************************************************
  9. Option Explicit
  10. '
  11. ' Required Win32 API Declarations
  12. '
  13. Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
  14. Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
  15. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
  16. Private Declare Function IsIconic Lib "user32" (ByVal hWnd As Long) As Long
  17. Private Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As Long) As Long
  18. Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
  19. Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
  20. '
  21. ' Constants used with APIs
  22. '
  23. Private Const SW_RESTORE = 9
  24. '
  25. ' Private variables needed to support enumeration
  26. '
  27. Private m_hWnd As Long
  28. Private m_Method As FindWindowPartialTypes
  29. Private m_CaseSens As Boolean
  30. Private m_Visible As Boolean
  31. Private m_AppTitle As String
  32. '
  33. ' Constants used by FindWindowPartial
  34. '
  35. Public Enum FindWindowPartialTypes
  36.    FwpStartsWith = 0
  37.    FwpContains = 1
  38.    FwpMatches = 2
  39. End Enum
  40.  
  41. Public Function AppActivatePartial(AppTitle As String, Optional Method As FindWindowPartialTypes = FwpStartsWith, Optional CaseSensitive As Boolean = False) As Long
  42.    Dim hWndApp As Long
  43.    '
  44.    ' Retrieve window handle for first top-level window
  45.    ' that starts with or contains the passed string.
  46.    '
  47.    hWndApp = FindWindowPartial(AppTitle, Method, CaseSensitive, True)
  48.    If hWndApp Then
  49.       '
  50.       ' Switch to it, restoring if need be.
  51.       '
  52.       If IsIconic(hWndApp) Then
  53.          Call ShowWindow(hWndApp, SW_RESTORE)
  54.       End If
  55.       Call SetForegroundWindow(hWndApp)
  56.       AppActivatePartial = hWndApp
  57.    End If
  58. End Function
  59.  
  60. Public Function FindWindowPartial(AppTitle As String, _
  61.    Optional Method As FindWindowPartialTypes = FwpStartsWith, _
  62.    Optional CaseSensitive As Boolean = False, _
  63.    Optional MustBeVisible As Boolean = False) As Long
  64.    '
  65.    ' Reset all search parameters.
  66.    '
  67.    m_hWnd = 0
  68.    m_Method = Method
  69.    m_CaseSens = CaseSensitive
  70.    m_AppTitle = AppTitle
  71.    '
  72.    ' Upper-case search string if case-insensitive.
  73.    '
  74.    If m_CaseSens = False Then
  75.       m_AppTitle = UCase$(m_AppTitle)
  76.    End If
  77.    '
  78.    ' Fire off enumeration, and return m_hWnd when done.
  79.    '
  80.    Call EnumWindows(AddressOf EnumWindowsProc, MustBeVisible)
  81.    FindWindowPartial = m_hWnd
  82. End Function
  83.  
  84. Private Function EnumWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
  85.    Static WindowText As String
  86.    Static nRet As Long
  87.    '
  88.    ' Make sure we meet visibility requirements.
  89.    '
  90.    If lParam Then 'window must be visible
  91.       If IsWindowVisible(hWnd) = False Then
  92.          EnumWindowsProc = True
  93.       End If
  94.    End If
  95.    '
  96.    ' Retrieve windowtext (caption)
  97.    '
  98.    WindowText = Space$(256)
  99.    nRet = GetWindowText(hWnd, WindowText, Len(WindowText))
  100.    If nRet Then
  101.       '
  102.       ' Clean up window text and prepare for comparison.
  103.       '
  104.       WindowText = Left$(WindowText, nRet)
  105.       If m_CaseSens = False Then
  106.          WindowText = UCase$(WindowText)
  107.       End If
  108.       '
  109.       ' Use appropriate method to determine if
  110.       ' current window's caption either starts
  111.       ' with, contains, or matches passed string.
  112.       '
  113.       Select Case m_Method
  114.          Case FwpStartsWith
  115.             If InStr(WindowText, m_AppTitle) = 1 Then
  116.                m_hWnd = hWnd
  117.             End If
  118.          Case FwpContains
  119.             If InStr(WindowText, m_AppTitle) <> 0 Then
  120.                m_hWnd = hWnd
  121.             End If
  122.          Case FwpMatches
  123.             If WindowText = m_AppTitle Then
  124.                m_hWnd = hWnd
  125.             End If
  126.       End Select
  127.    End If
  128.    '
  129.    ' Return True to continue enumeration if we haven't
  130.    ' found what we're looking for.
  131.    '
  132.    EnumWindowsProc = (m_hWnd = 0)
  133. End Function
  134.  
  135.