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 <>0
'取得下一个窗口的标题
GetWindowText hwnd, strTitle, Len(strTitle)
If Left$(strTitle, 1) <>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
:给你一个思路: 在代码中用到一个API函数 FindWindow,其命令格式为:FindWindow (类名,窗口名称)。 在Form中添加一个Timer控件灶知饥,将其Interval属性隐返设置为1000,在Timer事件中使用类似如下的代码: dim l as Long l=FindWindow ("","游戏窗口猛蠢以记事本为例Public Class Form1
Public Sub ShellAndWait(ByVal ProcessPath As String)
Dim objProcess As System.Diagnostics.Process
Try
objProcess = New System.Diagnostics.Process()
objProcess.StartInfo.FileName = ProcessPath
objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
objProcess.Start()
objProcess.WaitForExit()
objProcess.Close()
Catch
MessageBox.Show("无法执行文件 " &ProcessPath, "错误")
End Try
End Sub
'监视程序悄好拦就可以了,若果监视别的窗体的话,用SPY++ 找到句柄,配合FindWindowEx,SendMessage根启胡据其属性做
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ShellAndWait("Notepad.exe")
MessageBox.Show("笔记本被关袜液闭后我才会出现")
End Sub
End Class
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)