Private Sub Form_Load()
If AppPrevInstance Then MsgBox "程序已经运行!" : End
'你的其它代码
End Sub
'添加 Command1
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000
Const MAX_PATH As Integer = 260
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String MAX_PATH
End Type
Private Declare Function CreateToolhelp32Snapshot Lib "Kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long)
Dim chkfile$, aa$, r
Private Sub Form_Load()
MeAutoRedraw = True
Call addjclist
End Sub
Private Sub Command1_Click()
Call addjclist
chkfile = "Calcexe"
If InStr(aa, UCase(chkfile)) > 0 Then
rtn = MsgBox("您确定要终止 " & procname & " 吗?", vbYesNo, "强制关闭进程")
If rtn = 6 Then
SendMessage hProcess, WM_CLOSE, 0, 0 '关闭此进程
End If
Else
MsgBox chkfile & " 没运行"
End If
End Sub
Sub addjclist()
Dim hSnapShot As Long, uProcess As PROCESSENTRY32
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&)
uProcessdwSize = Len(uProcess)
r = Process32First(hSnapShot, uProcess)
aa = ""
Do While r
aa = aa & Trim(uProcessszExeFile) & " "
r = Process32Next(hSnapShot, uProcess)
Loop
aa = UCase(aa)
CloseHandle hSnapShot
End Sub
' 代码 2 简单的方式
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_CLOSE = &H10
Dim hProcess&, procname$
Private Sub Command1_Click()
procname = "计算器"
hProcess = FindWindow(vbNullString, procname)
If hProcess > 0 Then
rtn = MsgBox("您确定要终止 " & procname & " 吗?", vbYesNo, "强制关闭进程")
If rtn = 6 Then
SendMessage hProcess, WM_CLOSE, 0, 0 '关闭此进程
End If
Else
MsgBox procname & "没运行"
End If
End Sub
VB遍历进程查找窗口,这个过程很繁琐,而且代码量很大,所以我的代码用了巧妙的方法,在运行时在注册表保存了窗口句柄,这样就不用再查找了。测试通过,环境Win7+VB6:
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As LongConst SW_SHOW = 5
Private Sub Command1_Click()
MeHide
End Sub
Private Sub Form_Load()
Dim hwd As Long
If AppPrevInstance Then
MsgBox "程序已经运行!"
hwd = CLng(GetSetting(AppEXEName, "App", "MyHwnd"))
ShowWindow hwd, SW_SHOW
End
Else
SaveSetting AppEXEName, "App", "MyHwnd", Mehwnd
End If
End Sub
以上就是关于用VB怎么判断某一个应用程序运行!比如窗体LOAD事件,加载别的程序,当再次LOAD时提示程序已经运行!全部的内容,包括:用VB怎么判断某一个应用程序运行!比如窗体LOAD事件,加载别的程序,当再次LOAD时提示程序已经运行!、如何用VB来检测一个程序是否在运行,并且返回一个值、vb 检测程序是否正在运行,若正在运行则显示窗体等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)