怎样通过VB编程,实现监视某个窗口是否被打开,如果打开则强制关闭

怎样通过VB编程,实现监视某个窗口是否被打开,如果打开则强制关闭,第1张

添加塌孝一个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 <>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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12555547.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存