vb 只知道窗口类名,如何关闭窗口?
我这里有一段从网上摘来的源码,希望会的人能改一下。改成通过过指定的窗口类名,来关闭窗口。
'窗体源码:
'窗体Form1上分别放2个列表框List1,List2和1个计时器Timer1
Option Explicit
Private Sub Form_Load()
Form1.Caption = "游戏监控程序"
EnumWindows AddressOf EnumWindowsProc, ByVal 0&
Timer1.Enabled = True
Timer1.Interval = 5000 '5秒钟触发一次
End Sub
Private Sub Timer1_Timer()
Dim i As Integer, GameHandle As Long
'通过回调EnumWindowsProc遍历正在运行的所有顶级窗口
Form1.List1.Clear
EnumWindows AddressOf EnumWindowsProc, ByVal 0&
For i = 0 To List1.ListCount - 1
If InStr(1, List1.List(i), "纸牌") > 0 Then
GameHandle = Val(List1.List(i)) '纸牌游戏句柄
Call SendMessage(GameHandle, WM_CLOSE, ByVal 0&, ByVal 0&)
Form1.List2.AddItem "纸牌游戏易做图掉:" & List1.List(i)
End If
Next
For i = 0 To List1.ListCount - 1
If InStr(1, List1.List(i), "扫雷") > 0 Then
GameHandle = Val(List1.List(i)) '扫雷游戏句柄
Call SendMessage(GameHandle, WM_CLOSE, ByVal 0&, ByVal 0&)
Form1.List2.AddItem "扫雷游戏易做图掉:" & List1.List(i)
End If
Next
End Sub
'模块源码:
Option Explicit
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
Public Const WM_CLOSE As Long = &H10
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
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 GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim windowCaption As String, LengthCaption As Long 'windowCaption 为枚举到的窗口标题,
LengthCaption = GetWindowTextLength(hwnd) 'LengthCaption 为枚举到的窗口标题长度
windowCaption = Space(LengthCaption) '预设窗口标题长度(也是实际长度)
Call GetWindowText(hwnd, windowCaption, LengthCaption + 1) '取得窗口标题内容,内容保存在windowCaption中
Form1.List1.AddItem hwnd & " " & windowCaption '将枚举到的窗口句柄hWnd、窗口标题windowCaption添加到列表框控件List1中
'要干掉游戏窗口,请在Timer事件中定时检查列表框控件List1中出现的标题
EnumWindowsProc = True '回调结束返回真值True
End Function vb 关闭窗口 --------------------编程问答-------------------- 窗口Form1模块:
'窗体Form1上分别放2个列表框List1,List2和1个计时器Timer1
Option Explicit
Private Sub Form_Load()
Form1.Caption = "游戏监控程序"
EnumWindows AddressOf EnumWindowsProc, ByVal 0&
Timer1.Enabled = True
Timer1.Interval = 5000 '5秒钟触发一次
End Sub
Private Sub Timer1_Timer()
Dim i As Long 'Integer, GameHandle As Long
'通过回调EnumWindowsProc遍历正在运行的所有顶级窗口
Timer1.Enabled = False
Form1.List1.Clear
EnumWindows AddressOf EnumWindowsProc, ByVal 0&
For i = 0 To List1.ListCount - 1
If InStr(1, List1.List(i), "纸牌") > 0 Then
'GameHandle = Val(List1.List(i)) '纸牌游戏句柄
Call SendMessage(CLng(Val(List1.List(i))), WM_CLOSE, 0&, ByVal 0&)
Form1.List2.AddItem "纸牌游戏易做图掉:" & List1.List(i)
End If
Next
For i = 0 To List1.ListCount - 1
If InStr(1, List1.List(i), "扫雷") > 0 Then
'GameHandle = Val(List1.List(i)) '扫雷游戏句柄
Call SendMessage(CLng(Val(List1.List(i))), WM_CLOSE, 0&, ByVal 0&)
Form1.List2.AddItem "扫雷游戏易做图掉:" & List1.List(i)
End If
Next
Timer1.Enabled = True
End Sub
标准模块:
'模块源码:--------------------编程问答--------------------
Option Explicit
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
Public Const WM_CLOSE As Long = &H10
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
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 GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim windowCaption As String, LengthCaption As Long 'windowCaption 为枚举到的窗口标题,
LengthCaption = GetWindowTextLength(hwnd) 'LengthCaption 为枚举到的窗口标题长度
windowCaption = Space(LengthCaption) '预设窗口标题长度(也是实际长度)
Call GetWindowText(hwnd, windowCaption, LengthCaption + 1) '取得窗口标题内容,内容保存在windowCaption中
Form1.List1.AddItem hwnd & " " & windowCaption '将枚举到的窗口句柄hWnd、窗口标题windowCaption添加到列表框控件List1中
'要干掉游戏窗口,请在Timer事件中定时检查列表框控件List1中出现的标题
EnumWindowsProc = True '回调结束返回真值True
End Function
Private Sub Timer1_Timer()我自己以前写的一个模块,极方便
If window.getWindowHwndByTitle("纸牌") > 0 Then
window.closeWindow
End If
End Sub
补充:VB , 网络编程