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

如何通过程序标题检查程序是否正在运行?(使用vb6),vb6,process,Vb6,Process,如何通过程序标题检查程序是否正在运行?(使用vb6) 例如: 'check if there is a program contain a "Notepad" in its title if (does "Notepad" running now ?) then end if 上面的代码基本上取自 为此,必须知道确切的窗口标题。如果找到具有给定标题的窗口,函数将返回0,否则返回0 要查找标题包含特定字符串的窗口,您需要枚举所有窗口并自己查找正确的窗口。这个过程有点复杂,但在这里有详细的

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

例如:

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

if (does "Notepad" running now ?) then 

end if

上面的代码基本上取自

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


要查找标题包含特定字符串的窗口,您需要枚举所有窗口并自己查找正确的窗口。这个过程有点复杂,但在这里有详细的解释:。

传入应用程序的句柄和部分标题。如果找到,它将返回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
公共函数GetHandleFromPartialCaption(ByRef lWnd为长,ByVal sCaption为字符串)为布尔值
暗lhWndP为长
作为字符串的Dim sStr
GetHandleFromPartialCaption=False
lhWndP=FindWindow(vbNullString,vbNullString)'父窗口
当lhWndP为0时执行此操作
sStr=字符串(GetWindowTextLength(lhWndP)+1,Chr$(0))
GetWindowText lhWndP、sStr、Len(sStr)
sStr=左$(sStr,Len(sStr)-1)
如果仪表(1,sStr,sCaption)>0,则
GetHandleFromPartialCaption=True
lWnd=lhWndP
退出Do
如果结束
lhWndP=GetWindow(lhWndP,GW\U HWNDNEXT)
环

端函数

卡尔·彼得森在其出色的VB6网站上对此有一些介绍。它使用EnumWindows,如Tomalak的答案(在链接中)

现在右键单击任务栏,然后单击任务管理器。现在显示您正在运行的程序。

但我想检查是否有一个程序的标题中包含“记事本”,因为我不知道文档名是什么(无标题或任何其他内容)@faressoft:这就是为什么我在博客中添加了一个链接,解释了如何实现此功能。您还应该包括WIN API函数声明。
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