vb进程监控代码,程序关闭关闭后自动打开
添加一个Timer (Timer1) 和 一个按钮(Command1),然后添加以下代码。看看你还能不能打开窗口标题包含 计算 和 记事 的程序。 Option Explicit Private Declare Function GetDesktopWindow Lib "user32" () As Long Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long Private Declare Function GetWindowText Lib...全部
添加一个Timer (Timer1) 和 一个按钮(Command1),然后添加以下代码。看看你还能不能打开窗口标题包含 计算 和 记事 的程序。 Option Explicit Private Declare Function GetDesktopWindow Lib "user32" () As Long Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Private Const WM_CLOSE = &H10 Private Const GW_CHILD = 5 Private Const GW_HWNDNEXT = 2 Private Handle As Long '监视包含文字的数量。
Dim CloseCX(1) As String Private Sub Command1_Click() If Command1。Caption = "开始监视" Then Timer1。
Enabled = True Command1。Caption = "暂停监视" Else Command1。Caption = "开始监视" Timer1。Enabled = False End If End Sub Private Sub Form_Load() me。
visable=falsh Timer1。Enabled = False Timer1。Interval = 1000 Command1。Caption = "开始监视" '监视包含以下文字的窗口。
CloseCX(0) = "计算" CloseCX(1) = "记事" End Sub '该函数用来取得系统中所有打开的窗口 Public Function GetAllWindows() As Long '用来保存窗口的句柄 Dim hwnd As Long Dim Fx As Integer '取得桌面窗口 hwnd = GetDesktopWindow() '取得桌面窗口的第一个子窗口 hwnd = GetWindow(hwnd, GW_CHILD) Dim strTitle As String * 255 '用来存储窗口的标题 '通过循环来枚举所有的窗口 Do While hwnd vbNullChar Then For Fx = 0 To UBound(CloseCX, 1) If InStr(Left$(strTitle, InStr(1, strTitle, vbNullChar)), CloseCX(Fx)) > 0 Then '检查是否包含定义的字符 Handle = FindWindow(vbNullString, Left$(strTitle, InStr(1, strTitle, vbNullChar))) PostMessage Handle, WM_CLOSE, 0, 0 '如果包含 发送关闭命令。
End If Next End If '调用GetWindow函数,来取得下一个窗口 hwnd = GetWindow(hwnd, GW_HWNDNEXT) Loop End Function Private Sub Timer1_Timer() Call GetAllWindows End Sub。
收起