zoukankan      html  css  js  c++  java
  • C# WPF Webbrowser 强制所有网页链接在同一页面打开

    只要搞懂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;
            }
    
           
        }
  • 相关阅读:
    Datagrip导入导出为一个sql文件详细说明 (mysql)
    Linux/Unix/Mac OS下的远程访问和文件共享方式
    批量杀掉多个pid文件中记录的pid进程, 并集成到shell脚本中
    把tomcat服务器配置为windows服务的方法
    idea导入java项目
    linux-umount挂载点无法卸载:device is busy(解决)
    elasticsearch插件大全
    分布式搜索elasticsearch配置文件详解
    centos fastdfs 多服务器 多硬盘 多组 配置详解
    redis 配置 linux
  • 原文地址:https://www.cnblogs.com/T-ARF/p/10014101.html
Copyright © 2011-2022 走看看