打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Implementing a PopUp blocker into a WebBrowse...

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:

  • If the user initiated the action that leads to the new window
  • If the user holds the override key (the Ctrl Key)
  • If it is a pop-up displayed because of a window that is closing
  • The URL that is going to be opened
  • And more...

Using NewWindow3 clearly is very interesting for this purpose. To use this event, DWebBrowserEvents2 needs to be implemented.

Solution:

  1. Implement DWebBrowserEvents2
  2. Create a new event and a new event arguments class
  3. Launch this event with the appropriate information
  4. After the event is fired, see if the navigation needs to be canceled

 

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.

   

Code Snippet

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.

Code Snippet

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.

Code Snippet

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
IE webbrowser技巧集
人生如梦,你我皆过客,来去匆匆......
问一个SHDocVwCtl.WebBrowser的问题
MOX.cc [vb.net中webBrowser控件的问题?如何使弹出的网页在 本身 w...
C#调用WebBrowser NewWindow
WebBrowser控件的常用方法、属性和事件
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服