vb 如何判断外部程序是否运行

2024-11-24 10:12:28
推荐回答(2个)
回答1:

我拿任务管理器做实例
你在窗体中创建个timer 和两个cmmandbutton 把下面的代码复制进去直接可以运行
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Sub Command1_Click()
Shell "cmd /c call taskmgr", vbHide
End Sub

Private Sub Command2_Click()
Shell "cmd /c taskkill /im taskmgr.exe", vbHide
End Sub
Private Sub Form_Load()
Timer1.Interval = 100
Command1.Caption = "启动"
Command2.Caption = "关闭"
End Sub
Private Sub Timer1_Timer()
Cls
Dim hwnd As Long
hwnd = FindWindow(vbNullString, "windows 任务管理器") ' 取得进程标识符
If hwnd = 0 Then
Print "任务管理器未运行"
Else
Print "任务管理器已运行"
End If
End Sub

回答2:

findwindow
findwindowex