如何通过标题检查程序是否正在运行? (使用vb6)

时间:2011-01-03 15:46:41

标签: vb6 process

如何通过标题检查程序是否正在运行? (使用vb6)

示例:

'check if there is a program contain a "Notepad" in its title

if (does "Notepad" running now ?) then 

end if

alt text

4 个答案:

答案 0 :(得分:7)

''# preparation (in a separate module)
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Function FindWindowHandle(Caption As String) As Long
  FindWindowHandle = FindWindow(vbNullString, Caption)
End Function

''# use (anywhere)
MsgBox FindWindowHandle("Untitled - Notepad")

以上代码基本上取自here

必须知道确切的窗口标题。该函数将返回<>如果找到具有给定标题的窗口,则为0,否则为0.

要查找标题包含某个字符串的窗口,您需要枚举所有窗口并自行查找正确的窗口。这个过程稍微复杂一些,但在此详细解释:everythingaccess.com - Bring an external application window to the foreground

答案 1 :(得分:3)

Karl Peterson在他优秀的VB6网站上有一些great code。它像Enmalak的答案一样使用EnumWindows(在链接中)

答案 2 :(得分:1)

传递你的应用程序的句柄和部分标题。如果找到它将返回true。

Public Function GetHandleFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As Boolean

Dim lhWndP            As Long
Dim sStr              As String

GetHandleFromPartialCaption = False
lhWndP = FindWindow(vbNullString, vbNullString)                                      'PARENT WINDOW

Do While lhWndP <> 0
    sStr = String(GetWindowTextLength(lhWndP) + 1, Chr$(0))
    GetWindowText lhWndP, sStr, Len(sStr)
    sStr = Left$(sStr, Len(sStr) - 1)
    If InStr(1, sStr, sCaption) > 0 Then
        GetHandleFromPartialCaption = True
        lWnd = lhWndP
        Exit Do
    End If
    lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
Loop

结束功能

答案 3 :(得分:-3)

现在右键单击任务栏并单击任务管理器。现在展示你正在运行的程序。