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

C#WPFWebbrowser强制所有网页链接在同一页面打开

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

只要搞懂Winform的  WPF稍微改一改就可以了

主类:负责跳转的

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using static System.Windows.Forms.AxHost;

namespace Test
{

    public class WebBrowserUrl2
    {
        public String Url { get; }

        public String Frame { get; }

        public WebBrowserUrl2(String url, String frame)
        {
            this.Url = url;
            this.Frame = frame;
        }
    }
    public class WebBrowserEvent
    {
        public bool cancel;
    }


    public class WebbrowserOnNewWindow
    {
        static readonly BindingFlags info =
            BindingFlags.Instance |
            BindingFlags.Public |
            BindingFlags.NonPublic |
            BindingFlags.FlattenHierarchy |
            BindingFlags.CreateInstance;

        private WebBrowser web;
        public WebbrowserOnNewWindow(WebBrowser webBrowser)
        {
            web = webBrowser ?? throw new Exception();
            Excetue();
        }
        //跳转参考这个
        public delegate void OnNewWindow(WebBrowserUrl2 webBrowserUrl, WebBrowserEvent webBrowserEvent);

        public event OnNewWindow BeforeNewWidnow;

        private void Excetue()
        {

            var propertyAxiWebbrowser = web.GetType().GetProperty("AxIWebBrowser2", info);
            var axIWebBrowser2 = propertyAxiWebbrowser.GetValue(web, null);
            var webBrowserEvent = new NewWebBrowserEvent(this);
            var cookie = new ConnectionPointCookie(axIWebBrowser2, webBrowserEvent, typeof(INewDWWebBrowserEvent));
        }

        public void OnBeforeNewWindow(string url, out bool cancel)
        {
            var Arg = BeforeNewWidnow;
            WebBrowserUrl2 webBrowserUrl = new WebBrowserUrl2(url, null);
            WebBrowserEvent webBrowserEvent = new WebBrowserEvent();
            Arg?.Invoke(webBrowserUrl, webBrowserEvent);
            cancel = webBrowserEvent.cancel;
        }
    }

    public class NewWebBrowserEvent : System.Runtime.InteropServices.StandardOleMarshalObject, INewDWWebBrowserEvent
    {
        private WebbrowserOnNewWindow web;
        public NewWebBrowserEvent(WebbrowserOnNewWindow webbrowserOnNewWindow) => web = webbrowserOnNewWindow;

        public void BeforeNavigate2(object pDisp, ref object urlObject, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel)
        {
            //WPF这个和Winform差不多 没写
            //webBrowser.OnBeforeNavigate((string)urlObject, (string)targetFrameName, out cancel);
        }
        //官方说明此 事件是低于IE6时会引发
        public void NewWindow2(ref object ppDisp, ref bool cancel)
        {
            //WPF这个和Winform差不多 没写
            //  webBrowser.OnBeforeNewWindow(((WebBrowser)ppDisp).Url.ToString(), out cancel);
        }
        //当高于IE6时使用
        public void NewWindow3(object pDisp, ref bool cancel, ref object flags, ref object URLContext, ref object URL)
        {
            web.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 INewDWWebBrowserEvent
    {
        [System.Runtime.InteropServices.DispId(250)]
        void BeforeNavigate2(object pDisp, ref object urlObject, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel);
        //官方说明此 事件是低于IE6时会引发
        [System.Runtime.InteropServices.DispId(251)]
        void NewWindow2(ref object ppDisp, ref bool cancel);
        //当高于IE6时使用
        [System.Runtime.InteropServices.DispId(273)]
        void NewWindow3(object pDisp, ref bool cancel, ref object flags, ref object URLContext, ref object URL);
    }
}

XAML 界面

//省略其他

<Grid>
        <WebBrowser  x:Name="WB"  HorizontalAlignment="Left" Height="409" Margin="10,10,0,0" VerticalAlignment="Top" Width="655" Source="http://www.baidu.com" LoadCompleted="WB_LoadCompleted" />
    </Grid>

XAML.CS

  public partial class MainWindow : Window
    {
        private bool Start;
        public MainWindow()
        {
            InitializeComponent();
          
        }
        private void WB_LoadCompleted(object sender, NavigationEventArgs e)
        {
            if (!Start)
            { //这一步是没法省了
              //webbrowser在WPF中时密封类型
                WebbrowserOnNewWindow onNewWindow = new WebbrowserOnNewWindow(WB);
                onNewWindow.BeforeNewWidnow += OnNewWindow_BeforeNewWidnow;
                Start = true;
            }
        }
        private void OnNewWindow_BeforeNewWidnow(WebBrowserUrl2 webBrowserUrl, WebBrowserEvent webBrowserEvent)
        {
            WB.Navigate(new Uri(webBrowserUrl.Url));
            webBrowserEvent.cancel = true;
        }

       
    }

 


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
C#根据日期判断是星期几发布时间:2022-07-18
下一篇:
C#中的DataSet添加DataTable问题发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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