VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



You can use the WinWait function to wait until a window with a specific name has appeared, or use t

by Anonymous (267 Submissions)
Category: Windows System Services
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Thu 2nd May 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)

You can use the WinWait function to wait until a window with a specific name has appeared, or use the WinWaitNot function to wait until it is

API Declarations



Public bFound As Boolean
Public strWindowName As String

Public Const MAX_PATH = 260

Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Declare Function SendMessageArray Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Rate You can use the WinWait function to wait until a window with a specific name has appeared, or use t



    Dim lResult    As Long
    Dim lThreadId  As Long
    Dim lProcessId As Long
    Dim sWndName   As String
    Dim sClassName As String
    '
    ' This callback function is called by Windows (from the EnumWindows
    ' API call) for EVERY window that exists.  It populates the aWindowList
    ' array with a list of windows that we are interested in.
    '
    fEnumWindowsCallBack = 1
    sClassName = Space$(MAX_PATH)
    sWndName = Space$(MAX_PATH)
    
    lResult = GetClassName(hwnd, sClassName, MAX_PATH)
    sClassName = Left$(sClassName, lResult)
    lResult = GetWindowText(hwnd, sWndName, MAX_PATH)
    sWndName = Left$(sWndName, lResult)
    
    lThreadId = GetWindowThreadProcessId(hwnd, lProcessId)
    
    'Form1.lstWindows.AddItem CStr(hwnd) & vbTab & sClassName & _
        vbTab & CStr(lProcessId) & vbTab & CStr(lThreadId) & _
        vbTab & sWndName
        
    If UCase(sWndName) Like UCase(strWindowName) Then
      bFound = True
    End If
    

End Function

Public Function fEnumWindows() As Boolean
    Dim hwnd As Long
    '
    ' The EnumWindows function enumerates all top-level windows
    ' on the screen by passing the handle of each window, in turn,
    ' to an application-defined callback function. EnumWindows
    ' continues until the last top-level window is enumerated or
    ' the callback function returns FALSE.
    '
    Call EnumWindows(AddressOf fEnumWindowsCallBack, hwnd)
End Function


Public Function WinWait(strWinName As String, Optional intMaxWaitTimeInMin As Integer) As Boolean
  Dim OldTime As Date
  strWindowName = strWinName
  bFound = False
  If intMaxWaitTimeInMin > 0 Then
    OldTime = Date + Time
    Do While (bFound = False) And (DateDiff("n", OldTime, Date + Time) < intMaxWaitTimeInMin)
      DoEvents
      fEnumWindows
    Loop
  Else
    Do While bFound = False
      DoEvents
      fEnumWindows
    Loop
  End If
  
  If bFound = False Then
    WinWait = False
  Else
    WinWait = True
  End If
End Function

Public Function WinWaitNot(strWinName As String, Optional intMaxWaitTimeInMin As Integer) As Boolean
  Dim OldTime As Date
  strWindowName = strWinName
  
  If intMaxWaitTimeInMin > 0 Then
    OldTime = Date + Time
    Do
      bFound = False
      DoEvents
      fEnumWindows
    Loop While (bFound = True) And (DateDiff("n", OldTime, Date + Time) < intMaxWaitTimeInMin)
  Else
    Do
      bFound = False
      DoEvents
      fEnumWindows
    Loop While bFound = True
  End If
  
  WinWaitNot = bFound
End Function

Download this snippet    Add to My Saved Code

You can use the WinWait function to wait until a window with a specific name has appeared, or use t Comments

No comments have been posted about You can use the WinWait function to wait until a window with a specific name has appeared, or use t. Why not be the first to post a comment about You can use the WinWait function to wait until a window with a specific name has appeared, or use t.

Post your comment

Subject:
Message:
0/1000 characters