The main idea of Blocking unwanted pop-ups in that document:
Pop-ups are most of the time not very welcome, or could be inappropriate. To block these things, some additional information is needed. NewWindow3 gives this information when the user uses Windows XP SP2, or Windows 2003 SP1 or better. If this event is not fired, NewWindow2 takes its place. When NewWindow3 is fired, you can check:
Using NewWindow3 clearly is very interesting for this purpose. To use this event, DWebBrowserEvents2 needs to be implemented.
Solution:
Hi Travy,
Here are three more intuitionistic approaches:
1. Use the AxWebBrowser ActiveX control, and handle AxWebBrowser_NewWindow2 event and AxWebBrowser_NewWindow3 event.
Right-click the ToolBox -> select Choose Items -> Select the COM tab -> Select "Microsoft Web Browser"
The Microsoft Web Browser control will be added in the toolbox, then drag it onto your form.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AxWebBrowser1.Navigate("http://www.ccb.com")
End Sub
Private Sub AxWebBrowser1_NewWindow2(ByVal sender As System.Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_NewWindow2Event) Handles AxWebBrowser1.NewWindow2
e.cancel = True ' blocking pop up new window
End Sub
Private Sub AxWebBrowser1_NewWindow3(ByVal sender As System.Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_NewWindow3Event) Handles AxWebBrowser1.NewWindow3
e.cancel = True ' blocking pop up new window
End Sub
End Class
2. Use the AxWebBrowser ActiveX control, and handle AxWebBrowser_NavigateComplete2 event.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AxWebBrowser1.Navigate("http://www.ccb.com")
End Sub
Private Sub AxWebBrowser1_NavigateComplete2(ByVal sender As System.Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event) Handles AxWebBrowser1.NavigateComplete2
' Get a reference to the app that has opened in AxWebBrowser1 object
Dim oDocument As Object = e.pDisp.Document
oDocument.parentWindow.execScript("window.alert=null; ")
oDocument.parentWindow.execScript("window.confirm=null; ")
oDocument.parentWindow.execScript("window.showModalDialog=null; ")
oDocument.parentWindow.execScript("window.open=null; ")
End Sub
End Class
3. Using thread to monitor Popup windows and send message to close popup window.
Imports System.Runtime.InteropServices
Imports System.Threading
Public Class Form1
Private Const WM_SYSCOMMAND As Long = &H112
Private Const SC_CLOSE As Long = &HF060 ' Close window
' P/Invoke FindWindow API
<DllImport("User32.dll")> _
Public Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
End Function
' P/Invoke SendMessage API
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function SendMessage(ByVal hWnd As Integer, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("http://www.ccb.com")
' Run thread
Dim s As ThreadStart = New ThreadStart(AddressOf ClosePopup)
Dim myThread As Thread = New Thread(s)
myThread.Start()
End Sub
' Send Close message to the Popup window
Public Sub ClosePopup()
Dim handle As IntPtr
While True
handle = FindWindow(Nothing, "Popup Window Title")
If handle <> IntPtr.Zero Then
SendMessage(wordHandle, WM_SYSCOMMAND, SC_CLOSE, 0)
End If
End While
End Sub
End Class
Trackback: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2794291&SiteID=1
How to Maximize/Minimize/Restore/Close a window through SendMessage API?
Check this thread for reference:
http://www.msnewsgroups.net/group/microsoft.public.dotnet.languages.csharp/topic10556.aspx
Does WebBrowser control in .net 2.0 support blocking pop-up windows?
I hope that can help you.
Good luck!
Martin
聯(lián)系客服