我转一下。。 IE窗口的类名是IEFrame,用EnumWindows枚举所有顶级窗口,找到了,就关闭即可:模块: Option ExplicitPublic 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 Public Const WM_CLOSE = &H10 Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long 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 classname As String Dim str5 As String Dim len5 As Long, i As Long str5 = String(255, 0) len5 = 256 GetClassName hwnd, str5, 256 classname = Left(str5, InStr(1, str5, Chr(0)) - 1) Debug.Print classname If classname = "IEFrame" Then PostMessage hwnd, WM_CLOSE, 0&, 0& End If EnumWindowsProc = True End Function窗体,只有一个按钮: Option ExplicitPrivate Sub Command1_Click() EnumWindows AddressOf EnumWindowsProc, ByVal 0& End Sub
不用那么麻烦,用API出了错还不好调试。 Dim ieObj As InternetExplorer Dim swObj As ShellWindows Set swObj = New ShellWindows For Each ieObj In swObj ieObj.Quit NEXT
要添加对MICROSOFT INTETNET CONTROLS 的引用哦.
For Each ieObj In swObj ieObj.Quit NEXT页面多的时候(比如3个以上),用上面的代码关闭不完整,还是用枚举IE窗口或枚举进程的方法结束掉吧 用枚举进程的方法,找到IE进程,然后结束掉:Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function ExitProcess Lib "kernel32" (ByVal hObject 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 GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, ByVal lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long Private Declare Function GetCurrentProcess Lib "kernel32" () As Long Private Const LOCALE_STIMEFORMAT = &H1003 Private Const LOCALE_SSHORTDATE = &H1F Private Const WM_SETTINGCHANGE = &H1A Private Const HWND_BROADCAST = &HFFFF&Private Declare Function SetLocaleInfo Lib "kernel32" Alias "SetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String) As Boolean 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 Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long'获得进程的句柄 Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _ ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long '终止进程 Private Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, _ ByVal uExitCode As Long) As Long '创建一个系统快照 Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" _ (ByVal lFlags As Long, lProcessID As Long) As Long '获得系统快照中的第一个进程的信息 Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" _ (ByVal mSnapShot As Long, uProcess As PROCESSENTRY32) As Long '获得系统快照中的下一个进程的信息 Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" _ (ByVal mSnapShot As Long, uProcess As PROCESSENTRY32) As LongPrivate 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 * 260& End Type Private Const TH32CS_SNAPPROCESS As Long = 2& Dim mresult Private Sub Command1_Click() Dim uProcess As PROCESSENTRY32 Dim mSnapShot As Long Dim mName As String Dim mProcID As Long Dim i As Integer uProcess.dwSize = Len(uProcess) mSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&) If mSnapShot Then mresult = ProcessFirst(mSnapShot, uProcess) '失败则返回false Do While mresult i = InStr(1, uProcess.szexeFile, Chr(0)) mName = LCase$(Left$(uProcess.szexeFile, i - 1)) If UCase(mName) = UCase("iexplore.exe") Then mProcID = OpenProcess(1&, -1&, uProcess.th32ProcessID) '强行终止IE进程 TerminateProcess mProcID, 0& End If
mresult = ProcessNext(mSnapShot, uProcess) Loop End If End Sub
引用 Microsoft Internet ControlsPrivate Sub Command1_Click() Dim ieWIN As ShellWindows Dim ie As InternetExplorer Set ieWIN = New ShellWindows For Each ie In ieWIN ie.Quit Next End Sub如果一次关闭不了就运行2次,哈哈
IE窗口的类名是IEFrame,用EnumWindows枚举所有顶级窗口,找到了,就关闭即可:模块:
Option ExplicitPublic 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
Public Const WM_CLOSE = &H10
Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
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 classname As String
Dim str5 As String
Dim len5 As Long, i As Long
str5 = String(255, 0)
len5 = 256
GetClassName hwnd, str5, 256
classname = Left(str5, InStr(1, str5, Chr(0)) - 1)
Debug.Print classname
If classname = "IEFrame" Then
PostMessage hwnd, WM_CLOSE, 0&, 0&
End If
EnumWindowsProc = True
End Function窗体,只有一个按钮:
Option ExplicitPrivate Sub Command1_Click()
EnumWindows AddressOf EnumWindowsProc, ByVal 0&
End Sub
Dim ieObj As InternetExplorer
Dim swObj As ShellWindows Set swObj = New ShellWindows
For Each ieObj In swObj
ieObj.Quit
NEXT
ieObj.Quit
NEXT页面多的时候(比如3个以上),用上面的代码关闭不完整,还是用枚举IE窗口或枚举进程的方法结束掉吧
用枚举进程的方法,找到IE进程,然后结束掉:Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function ExitProcess Lib "kernel32" (ByVal hObject 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 GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, ByVal lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Const LOCALE_STIMEFORMAT = &H1003
Private Const LOCALE_SSHORTDATE = &H1F
Private Const WM_SETTINGCHANGE = &H1A
Private Const HWND_BROADCAST = &HFFFF&Private Declare Function SetLocaleInfo Lib "kernel32" Alias "SetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String) As Boolean
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 Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long'获得进程的句柄
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
'终止进程
Private Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, _
ByVal uExitCode As Long) As Long
'创建一个系统快照
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" _
(ByVal lFlags As Long, lProcessID As Long) As Long
'获得系统快照中的第一个进程的信息
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" _
(ByVal mSnapShot As Long, uProcess As PROCESSENTRY32) As Long
'获得系统快照中的下一个进程的信息
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" _
(ByVal mSnapShot As Long, uProcess As PROCESSENTRY32) As LongPrivate 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 * 260&
End Type
Private Const TH32CS_SNAPPROCESS As Long = 2&
Dim mresult
Private Sub Command1_Click()
Dim uProcess As PROCESSENTRY32
Dim mSnapShot As Long
Dim mName As String
Dim mProcID As Long
Dim i As Integer uProcess.dwSize = Len(uProcess)
mSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
If mSnapShot Then
mresult = ProcessFirst(mSnapShot, uProcess)
'失败则返回false
Do While mresult
i = InStr(1, uProcess.szexeFile, Chr(0))
mName = LCase$(Left$(uProcess.szexeFile, i - 1)) If UCase(mName) = UCase("iexplore.exe") Then
mProcID = OpenProcess(1&, -1&, uProcess.th32ProcessID)
'强行终止IE进程
TerminateProcess mProcID, 0&
End If
mresult = ProcessNext(mSnapShot, uProcess)
Loop
End If
End Sub
Dim ieWIN As ShellWindows
Dim ie As InternetExplorer
Set ieWIN = New ShellWindows
For Each ie In ieWIN
ie.Quit
Next
End Sub如果一次关闭不了就运行2次,哈哈