【问题标题】:how to disallow disable open page in new window - cefsharp如何禁止在新窗口中禁用打开页面 - cefsharp
【发布时间】:2021-02-12 05:28:37
【问题描述】:

我尝试了很多,但无法成功:(。 我想在同一个主窗口而不是新窗口中打开第二个窗口。

我尝试了以下但没有运气,也许我做错了什么,因为我不擅长编码:(

https://github.com/cefsharp/CefSharp/issues/600

https://github.com/cefsharp/CefSharp/issues/688

https://www.codeproject.com/Articles/1194609/Capturing-a-pop-up-window-using-LifeSpanHandler-an

您能帮我找到解决方案,因此无论您在任何网页上单击什么按钮,它都只在窗口中打开

using CefSharp;
using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Microsoft.Win32;


namespace Nysus
{

  
public partial class Form1 : Form
    {
        public Form1()
        {
             InitializeComponent();
        }

        ChromiumWebBrowser chrome;
        private void Form1_Load(object sender, EventArgs e)
        {
            CefSettings settings = new CefSettings();
            //Initialize
            Cef.EnableHighDPISupport();
            Cef.Initialize(settings);
            chrome = new ChromiumWebBrowser("http://www.google.com");
            this.pContainer.Controls.Add(chrome);
            chrome.Dock = DockStyle.Fill;
          
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;
            if (chrome.CanGoForward)
                chrome.Forward();
            if (chrome.CanGoBack)
                chrome.Back();
        }
       
        private void Form1_FormClosing_1(object sender, FormClosingEventArgs e)
        {
            Cef.Shutdown();
        }

        public bool OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
        {
            browser.MainFrame.LoadUrl(targetUrl);
            newBrowser = null;
            return true;
        }

    }
}

【问题讨论】:

    标签: c# visual-studio winforms cefsharp


    【解决方案1】:

    如果您想要强制每个目标空白链接在同一个浏览器窗口中打开,那么 LifeSpanHandler 是可行的方法(考虑到您无权访问源 HTML)。 OnBeforePopup 方法在这种情况下有效,但您在无意义的地方添加了该方法。创建一个扩展 ILifeSpanHandler 类的新类:

    // MyCustomLifeSpanHandler.cs
    using CefSharp;
    
    public class MyCustomLifeSpanHandler : ILifeSpanHandler
    {
        // Load new URL (when clicking a link with target=_blank) in the same frame
        public bool OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
        {
            browser.MainFrame.LoadUrl(targetUrl);
            newBrowser = null;
            return true;
        }
    } 
    

    然后在浏览器初始化期间将 LifeSpanHandler 替换为自定义的(使用您的代码):

    ChromiumWebBrowser chrome;
    private void Form1_Load(object sender, EventArgs e)
    {
        CefSettings settings = new CefSettings();
        //Initialize
        Cef.EnableHighDPISupport();
        Cef.Initialize(settings);
        chrome = new ChromiumWebBrowser("http://www.google.com");
    
        // Implement Custom LifeSpanHandler
        chrome.LifeSpanHandler = new MyCustomLifeSpanHandler();
        // End Implement Custom LifeSpanHandler
    
        this.pContainer.Controls.Add(chrome);
        chrome.Dock = DockStyle.Fill;
        
        this.FormBorderStyle = FormBorderStyle.None;
        this.WindowState = FormWindowState.Maximized;
        if (chrome.CanGoForward)
            chrome.Forward();
        if (chrome.CanGoBack)
            chrome.Back();
    }
    

    这应该可行。你可以找到whole example in this article here

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多