How to check if a task exists by searching for (part or all of) the window title
Posted on August 22, 2011
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 'System & API - How to check if a task exists by searching for (part or all of) the window title 'Constants & Declarations Dealing with Running Tasks Public Const GW_HWNDNEXT = 2 '\2 = next window handle 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 GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" _ (ByVal hwnd As Long) As Long Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long Public Declare Function GetActiveWindow Lib "user32" () As Long 'This function checks if a task exists by searching for (part or all of) the window title. Public Function TaskExists(ByVal strCheckTask As String, ByVal ExactMatch As Boolean) As Boolean Dim CurrWnd As Long Dim Length As Long Dim TaskName As String TaskExists = False CurrWnd = GetActiveWindow() Do While (CurrWnd <> 0) Length = GetWindowTextLength(CurrWnd) TaskName = Space$(Length + 1) Length = GetWindowText(CurrWnd, TaskName, Length + 1) TaskName = Left$(TaskName, Len(TaskName) - 1) If (Length > 0) Then If ExactMatch = True Then 'task gelijk aan string If (TaskName = strCheckTask) Then TaskExists = True Exit Do End If Else If InStr(TaskName, strCheckTask) Then TaskExists = True Exit Do End If End If End If CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT) DoEvents Loop End Function 'How can I call this function 'MsgBox TaskExists("NotePad", False) |