VB 用Command结束程序
这是源代码,主题是想让一个计时算数程序结束PrivateSubCommand2_Click()IfCommand1.Caption="开始"ThenTimer1.Enab...
这是源代码,主题是想让一个计时算数程序结束Private Sub Command2_Click()If Command1.Caption = "开始" Then
Timer1.Enabled = TrueLabel5.Caption = "程序终止"Label1.Caption = ""Text1.Text = ""Label6.Width = 0Label9.Caption = "00:00"EndEnd If
End Sub 展开
Timer1.Enabled = TrueLabel5.Caption = "程序终止"Label1.Caption = ""Text1.Text = ""Label6.Width = 0Label9.Caption = "00:00"EndEnd If
End Sub 展开
2个回答
展开全部
Private Sub Command2_Click()
If Command1.Caption = "开始" ThenTimer1.Enabled = True ****************这里改成false
Label5.Caption = "程序终止"
Label1.Caption = ""
Text1.Text = ""
Label6.Width = 0
Label9.Caption = "00:00"
End******************这个会是程序强行退出,可以不用
End If
If Command1.Caption = "开始" ThenTimer1.Enabled = True ****************这里改成false
Label5.Caption = "程序终止"
Label1.Caption = ""
Text1.Text = ""
Label6.Width = 0
Label9.Caption = "00:00"
End******************这个会是程序强行退出,可以不用
End If
更多追问追答
追问
不行啊,这个Command还是灰色的不能用状态啊
追答
end 前一行加上 Command1.enabled=true
展开全部
添加三个CommandButton和一个ListBox
Option ExplicitDim ProcessID() As Long ' 按list1中的进程顺序存储所有进程ID'---------- API类型声明 -----------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 * 1024End TypePrivate Type MODULEENTRY32 '模块 dwsize As Long th32ModuleID As Long th32ProcessID As Long GlblcntUsage As Long ProccntUsage As Long modBaseAddr As Byte modBaseSize As Long hModule As Long szModule As String * 256 szExePath As String * 1024End TypePrivate Type THREADENTRY32 '线程 dwsize As Long cntusage As Long th32threadID As Long th32OwnerProcessID As Long tpBasePri As Long tpDeltaPri As Long dwFlags As LongEnd Type'----------------------------------------- API声明 -------------------------------------------------------Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As LongPrivate Declare Function Module32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As MODULEENTRY32) As LongPrivate Declare Function Module32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As MODULEENTRY32) As LongPrivate Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As LongPrivate Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As LongPrivate Declare Function Thread32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As LongPrivate Declare Function Thread32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As LongPrivate Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As LongPrivate Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPrivate Declare Function GetCurrentProcessId Lib "kernel32" () As Long'---------------------------------------- API常数声明 ------------------------------------------------------Private Const TH32CS_SNAPHEAPLIST = &H1Private Const TH32CS_SNAPPROCESS = &H2Private Const TH32CS_SNAPTHREAD = &H4Private Const TH32CS_SNAPMODULE = &H8Private Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)Private Const TH32CS_INHERIT = &H80000000Private Const PROCESS_TERMINATE = &H1&Private Sub Command1_Click()Dim Process As PROCESSENTRY32Dim ProcSnap As LongDim cntProcess As LongcntProcess = 0List1.ClearProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)If ProcSnap Then Process.dwsize = 1060 ' 通常用法 Process32First ProcSnap, Process Do Until Process32Next(ProcSnap, Process) < 1 ' 遍历所有进程直到返回值为False List1.AddItem Trim(Process.szExeFile) cntProcess = cntProcess + 1 LoopEnd IfReDim ProcessID(cntProcess) As LongDim i As Longi = 0Process32First ProcSnap, ProcessDo Until Process32Next(ProcSnap, Process) < 1 ' 遍历所有进程直到返回值为False ProcessID(i) = Process.th32ProcessID i = i + 1LoopCloseHandle (ProcSnap)End SubPrivate Sub Command2_Click()Dim c As IntegerIf List1.ListIndex < 0 Then MsgBox "请选择进程!", vbOKOnly + vbInformation, "提示"Else Dim hProcess As Long hProcess = OpenProcess(PROCESS_TERMINATE, False, ProcessID(List1.ListIndex)) If hProcess Then TerminateProcess hProcess, 0 c = List1.ListCount While List1.ListCount = c Command1_Click WendEnd IfEnd SubPrivate Sub Command3_Click()Unload MeEnd Sub
Option ExplicitDim ProcessID() As Long ' 按list1中的进程顺序存储所有进程ID'---------- API类型声明 -----------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 * 1024End TypePrivate Type MODULEENTRY32 '模块 dwsize As Long th32ModuleID As Long th32ProcessID As Long GlblcntUsage As Long ProccntUsage As Long modBaseAddr As Byte modBaseSize As Long hModule As Long szModule As String * 256 szExePath As String * 1024End TypePrivate Type THREADENTRY32 '线程 dwsize As Long cntusage As Long th32threadID As Long th32OwnerProcessID As Long tpBasePri As Long tpDeltaPri As Long dwFlags As LongEnd Type'----------------------------------------- API声明 -------------------------------------------------------Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As LongPrivate Declare Function Module32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As MODULEENTRY32) As LongPrivate Declare Function Module32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As MODULEENTRY32) As LongPrivate Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As LongPrivate Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As LongPrivate Declare Function Thread32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As LongPrivate Declare Function Thread32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As LongPrivate Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As LongPrivate Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPrivate Declare Function GetCurrentProcessId Lib "kernel32" () As Long'---------------------------------------- API常数声明 ------------------------------------------------------Private Const TH32CS_SNAPHEAPLIST = &H1Private Const TH32CS_SNAPPROCESS = &H2Private Const TH32CS_SNAPTHREAD = &H4Private Const TH32CS_SNAPMODULE = &H8Private Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)Private Const TH32CS_INHERIT = &H80000000Private Const PROCESS_TERMINATE = &H1&Private Sub Command1_Click()Dim Process As PROCESSENTRY32Dim ProcSnap As LongDim cntProcess As LongcntProcess = 0List1.ClearProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)If ProcSnap Then Process.dwsize = 1060 ' 通常用法 Process32First ProcSnap, Process Do Until Process32Next(ProcSnap, Process) < 1 ' 遍历所有进程直到返回值为False List1.AddItem Trim(Process.szExeFile) cntProcess = cntProcess + 1 LoopEnd IfReDim ProcessID(cntProcess) As LongDim i As Longi = 0Process32First ProcSnap, ProcessDo Until Process32Next(ProcSnap, Process) < 1 ' 遍历所有进程直到返回值为False ProcessID(i) = Process.th32ProcessID i = i + 1LoopCloseHandle (ProcSnap)End SubPrivate Sub Command2_Click()Dim c As IntegerIf List1.ListIndex < 0 Then MsgBox "请选择进程!", vbOKOnly + vbInformation, "提示"Else Dim hProcess As Long hProcess = OpenProcess(PROCESS_TERMINATE, False, ProcessID(List1.ListIndex)) If hProcess Then TerminateProcess hProcess, 0 c = List1.ListCount While List1.ListCount = c Command1_Click WendEnd IfEnd SubPrivate Sub Command3_Click()Unload MeEnd Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询