'工程中引用Microsoft Internet ControlsOption Explicit Private WithEvents Timer1 As Timer Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Public mDocument As Object Const WM_CLOSE = &H10 Dim phwnd&, rtn&, i& Private Sub Form_Load() Set Timer1 = Controls.Add("vb.timer", "Timer1") Timer1.Interval = 1000 Timer1.Enabled = True End SubPrivate Sub Timer1_Timer() Call mComGetIEWindows End SubPublic Sub mComGetIEWindows() On Error Resume Next Timer1.Enabled = False Dim mShellWindow As New SHDocVw.ShellWindows For i = 0 To mShellWindow.Count - 1 If VBA.TypeName(mShellWindow.Item(i).Document) = "HTMLDocument" Then If InStr(UCase(mShellWindow.Item(i).Document.url), "SOHU") > 0 Then phwnd = mShellWindow.Item(i).hwnd PostMessage phwnd, WM_CLOSE, 0, 0 '关闭 Call Shell("explorer http://www.sina.com.cn/", vbNormalFocus) Exit For End If End If Next i Timer1.Enabled = True End Sub
1.得到IE窗口.
2.枚举每个控件句柄.
3.检查每个句柄中的英文字母,然后对比过滤关键字.发现即关闭80端口如果LZ的话是否可以将第三条改一下,得到过滤关键字即向控件发送消息:www.sina.com.cn
Private WithEvents Timer1 As Timer
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public mDocument As Object
Const WM_CLOSE = &H10
Dim phwnd&, rtn&, i&
Private Sub Form_Load()
Set Timer1 = Controls.Add("vb.timer", "Timer1")
Timer1.Interval = 1000
Timer1.Enabled = True
End SubPrivate Sub Timer1_Timer()
Call mComGetIEWindows
End SubPublic Sub mComGetIEWindows()
On Error Resume Next
Timer1.Enabled = False
Dim mShellWindow As New SHDocVw.ShellWindows
For i = 0 To mShellWindow.Count - 1
If VBA.TypeName(mShellWindow.Item(i).Document) = "HTMLDocument" Then
If InStr(UCase(mShellWindow.Item(i).Document.url), "SOHU") > 0 Then
phwnd = mShellWindow.Item(i).hwnd
PostMessage phwnd, WM_CLOSE, 0, 0 '关闭
Call Shell("explorer http://www.sina.com.cn/", vbNormalFocus)
Exit For
End If
End If
Next i
Timer1.Enabled = True
End Sub
呵呵