• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#中的webBrowser空间连接InvokeMember("click")无效问题 ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

首先要确定获取到了连接对象的前提下

 

由于设置了取消新窗口打开

private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
e.Cancel=true;
}

 

因此在webbrowser 中的 弹出新窗口的连接都被屏蔽了 显示无效

方法一、

 //将所有的链接的目标,指向本窗体
    foreach (HtmlElement archor in this.webBrowser1.Document.Links)
    {
        archor.SetAttribute("target", "_self");
    }

    //将所有的FORM的提交目标,指向本窗体
    foreach (HtmlElement form in this.webBrowser1.Document.Forms)
    {
        form.SetAttribute("target", "_self");
    }

  


将 WebBrowser 的 AllowWebBrowserDrop 设为 false
将 WebBrowser 的 WebBrowserShortcutsEnabled 设为 false
将 WebBrowser 的 IsWebBrowserContextMenuEnabled 设为 false


方法二

在NewWindow事件中取得要打开的网址,取消打开新窗口,然后在本窗口打开要转向的网址
private void webBrowser_1_NewWindow(object sender, CancelEventArgs e) 
{ 
WebBrowser webBrowser_temp = (WebBrowser)sender;
string newUrl = webBrowser_temp.Document.ActiveElement.GetAttribute("href"); 
webBrowser_1.Url = new Uri(newUrl); 
e.Cancel = true; 
}

  

 

虽然这种方法能够解决大部分的要求,但是治标不治本。
而且当网址里面还有汉字的时候就更加麻烦了,比如在百度MP3,歌曲名字都是汉字,和百度空间里面,大部分用户名都是汉字,所以取的网址都用乱码。虽然,可以对网址进行编码,但是并不是所有网页都是uft-8编码,对于如何得知网页的编码又是一个课题了。

 

方法三

 

最根本的方法就是重写了。
新建一个类

 

 

using System;
using System.Collections.Generic;
using System.Text;

namespace webTestRecorder
{

    public class ExtendedWebBrowser : System.Windows.Forms.WebBrowser
    {
        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<WebBrowserExtendedNavigatingEventArgs> BeforeNavigate;
        public event EventHandler<WebBrowserExtendedNavigatingEventArgs> BeforeNewWindow;

        protected void OnBeforeNewWindow(string url, out bool cancel)
        {
            EventHandler<WebBrowserExtendedNavigatingEventArgs> 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<WebBrowserExtendedNavigatingEventArgs> 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
        {
            ExtendedWebBrowser _Browser;
            public WebBrowserExtendedEvents(ExtendedWebBrowser 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 : System.ComponentModel.CancelEventArgs
    {
        p rivate string _Url;
        public string Url
        {
            get { return _Url; }
        }

        p rivate string _Frame;
        public string Frame
        {
            get { return _Frame; }
        }

        public WebBrowserExtendedNavigatingEventArgs(string url, string frame)
            : base()
        {
            _Url = url;
            _Frame = frame;
        }
    }
}

 

 

然后把webBrowser换成我们重写的ExtendedWebBrowser,添加事件处理


 ieBrowser = new ExtendedWebBrowser();
ieBrowser.BeforeNewWindow += new EventHandler<WebBrowserExtendedNavigatingEventArgs>(ieBrowser_BeforeNewWindow);

然后在其BeforeNewWindow事件中:

void ieBrowser_BeforeNewWindow(object sender, WebBrowserExtendedNavigatingEventArgs e) {
     e.Cancel=true;
     ((ExtendedWebBrowser)sender).Navigate(e.Url);
}

 

  

 

 

 



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#上传文件到服务器指定文件夹问题发布时间:2022-07-10
下一篇:
C#获取一段日期内的工作日发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap