开发者

How to check if a program is running now or not by its title ? (using vb6)

开发者_运维知识库How to check if a program is running now or not by its title ? (using vb6)

Example :

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

if (does "Notepad" running now ?) then 

end if

How to check if a program is running now or not by its title ? (using vb6)


''# 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")

Code above basically taken from here.

The exact window caption must be known for this. The function will return <> 0 if a window with the given caption was found, 0 otherwise.

To find a window whose caption contains a certain string, you will need to enumerate all windows and look for the correct one yourself. This process is a little more complicated, but explained in great detail here: everythingaccess.com - Bring an external application window to the foreground.


Karl Peterson has some great code for this on his excellent VB6 website. It uses EnumWindows like Tomalak's answer (in the link)


Pass in the handle of your app and a partial caption. It will return true if found.

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

End Function


Now right click on taskbar and click task manager. Now show your running program.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜