程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> 關於ASP.NET >> WebBrowser控件使用技巧分享

WebBrowser控件使用技巧分享

編輯:關於ASP.NET

首先分享一個WebBrowser的擴展類(此類所需的dll將在文章末尾提供下載),大家最好 都使用這個類來替代.Net框架中的WebBrowser類,它提供了兩個擴展功能:

1.屏蔽錯誤腳本提示。修正了WebBrowser控件本身屏蔽錯誤不全的問題,由啟明提出,原 文:http://www.cnblogs.com/hobe/archive/2007/01/14/619906.html

2.擴展NewWindow事件。修正了WebBrowser控件本身的NewWindow事件不提供新窗口Url的 問題,通過新增的BeforeNewWindow事件予以支持,由佳文轉載並整理,原文: http://www.cnblogs.com/yjwgood/archive/2009/02/09/1386789.html

整合後的代碼如下:

public class ExWebBrowser : System.Windows.Forms.WebBrowser
{
    private SHDocVw.IWebBrowser2 Iwb2;
    protected override void AttachInterfaces(object nativeActiveXObject)
    {
        Iwb2 = (SHDocVw.IWebBrowser2)nativeActiveXObject;
        Iwb2.Silent = true;
        base.AttachInterfaces(nativeActiveXObject);
    }
    protected override void DetachInterfaces()
    {
        Iwb2 = null;
        base.DetachInterfaces();
    }
    System.Windows.Forms.AxHost.ConnectionPointCookie cookie;
    WebBrowserExtendedEvents events;
    //This method will be called to give you a chance to create your own event sink
    protected override void CreateSink()
    {
        //MAKE SURE TO CALL THE BASE or the normal events won't fire
        base.CreateSink();
        events = new WebBrowserExtendedEvents(this);
        cookie = new System.Windows.Forms.AxHost.ConnectionPointCookie (this.ActiveXInstance, events, typeof(DWebBrowserEvents2));
    }
    protected override void DetachSink()
    {
        if (null != cookie)
        {
            cookie.Disconnect();
            cookie = null;
        }
        base.DetachSink();
    }
    //This new event will fire when the page is navigating
    public event EventHandler BeforeNavigate;
    /// <summary>
    /// 可用於替代原來的NewWindow事件,新增了事件的Url參數支持。
    /// </summary>
    [CategoryAttribute("操作"), DescriptionAttribute("經過擴展的NewWindow事 件,使用繼承後的WebBrowserExtendedNavigatingEventArgs類型參數實現Url參數支持")]
    public event EventHandler BeforeNewWindow;
    protected void OnBeforeNewWindow(string url, out bool cancel)
    {
        EventHandler h = BeforeNewWindow;
        WebBrowserExtendedNavigatingEventArgs args = new WebBrowserExtendedNavigatingEventArgs(url, null);
        if (null != h)
        {
            h(this, args);
        }
        cancel = args.Cancel;
    }
    protected void OnBeforeNavigate(string url, string frame, out bool cancel)
    {
        EventHandler h = BeforeNavigate;
        WebBrowserExtendedNavigatingEventArgs args = new WebBrowserExtendedNavigatingEventArgs(url, frame);
        if (null != h)
        {
            h(this, args);
        }
        //Pass the cancellation chosen back out to the events
        cancel = args.Cancel;
    }
    //This class will capture events from the WebBrowser
    class WebBrowserExtendedEvents : System.Runtime.InteropServices.StandardOleMarshalObject, DWebBrowserEvents2
    {
        ExWebBrowser _Browser;
        public WebBrowserExtendedEvents(ExWebBrowser browser) { _Browser = browser; }
        //Implement whichever events you wish
        public void BeforeNavigate2(object pDisp, ref object URL, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel)
        {
            _Browser.OnBeforeNavigate((string)URL, (string) targetFrameName, out cancel);
        }
        public void NewWindow3(object pDisp, ref bool cancel, ref object flags, ref object URLContext, ref object URL)
        {
            _Browser.OnBeforeNewWindow((string)URL, out cancel);
        }
    }
    [System.Runtime.InteropServices.ComImport(), System.Runtime.InteropServices.Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"),
    System.Runtime.InteropServices.InterfaceTypeAttribute (System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch),
    System.Runtime.InteropServices.TypeLibType (System.Runtime.InteropServices.TypeLibTypeFlags.FHidden)]
    public interface DWebBrowserEvents2
    {
        [System.Runtime.InteropServices.DispId(250)]
        void BeforeNavigate2(
            [System.Runtime.InteropServices.In,
            System.Runtime.InteropServices.MarshalAs (System.Runtime.InteropServices.UnmanagedType.IDispatch)] object pDisp,
            [System.Runtime.InteropServices.In] ref object URL,
            [System.Runtime.InteropServices.In] ref object flags,
            [System.Runtime.InteropServices.In] ref object targetFrameName, [System.Runtime.InteropServices.In] ref object postData,
            [System.Runtime.InteropServices.In] ref object headers,
            [System.Runtime.InteropServices.In,
            System.Runtime.InteropServices.Out] ref bool cancel);
        [System.Runtime.InteropServices.DispId(273)]
        void NewWindow3(
            [System.Runtime.InteropServices.In,
            System.Runtime.InteropServices.MarshalAs (System.Runtime.InteropServices.UnmanagedType.IDispatch)] object pDisp,
            [System.Runtime.InteropServices.In, System.Runtime.InteropServices.Out] ref bool cancel,
            [System.Runtime.InteropServices.In] ref object flags,
            [System.Runtime.InteropServices.In] ref object URLContext,
            [System.Runtime.InteropServices.In] ref object URL);
    }
}
public class WebBrowserExtendedNavigatingEventArgs : CancelEventArgs
{
    private string _Url;
    public string Url
    {
        get { return _Url; }
    }
    private string _Frame;
    public string Frame
    {
        get { return _Frame; }
    }
    public WebBrowserExtendedNavigatingEventArgs(string url, string frame)
        : base()
    {
        _Url = url;
        _Frame = frame;
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved