0

I am trying to convert our current WebForms app to use CefSharp browser which uses ChromimumWebBrowser. I came across the below in current code:

private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
    if (!newWindow3ListenerAdded) {        
        newWindow3ListenerAdded = true;
        SHDocVw.WebBrowser browser = (SHDocVw.WebBrowser)webBrowser.ActiveXInstance;        
        browser.NewWindow3 += Browser_NewWindow3;
        browser.NewWindow2 += browser_NewWindow2;
    }
}

private void browser_NewWindow2(ref object ppDisp, ref bool Cancel)
{
    webform.webBrowser.GoBack();
    ppDisp = webform.webBrowser.ActiveXInstance;
    webform.Show();
}

private void Browser_NewWindow3(ref object ppDisp, ref bool cancel, uint dwFlags, string sourceUrl, string targetUrl) 
{
    cancel = true;
    if (targetUrl.Contains(".pdf")) {
        WebForm webForm = new WebForm();
        webForm.Text = targetUrl;
        webForm.Navigate(targetUrl);
        webForm.Show();
    } else {
        webBrowser.Navigate(targetUrl);
    }
}

Could someone please guide me in converting this to Cef. I know I have to implement OnBeforePopup() in ILifeSpanHandler. But how exactly can I write the logic to load pdf/GoBack/Navigate(targetUrl) for different types of popups.

I think I can easily convert Browser_NewWindow3() the way it is (checking if targetUrl.Contains(".pdf")) but on what condition do I handle browser_NewWindow2() logic i.e. webBrowser.GoBack(); ?

This is what I tried,

OnBeforePopup() {
newBrowser = null;
    if (targetUrl.Contains(".pdf")) {
       CefForm cefForm = new CefForm();
       cefForm.Text = targetUrl;
       cefForm.Navigate(targetUrl);
       cefForm.Show();
       return false;
    } else {
       browserControl.Load(targetUrl);
       return true;
    }
}

But how do I handle browser_NewWindow2 logic ?

HeXMaN
  • 113
  • 1
  • 11
  • I'm not familiar with SHDocVw.WebBrowser. What does your ILifeSpanHandler.OnBeforePopup implementation look like currently? What does browser_NewWindow2 achieve? Run your app with the code and run it again with the code comment out, work out what the behaviour is. – amaitland May 06 '21 at 20:02
  • Updated in the question what I tried. Do you think browserControl.Load(targetUrl); is correct ? – HeXMaN May 07 '21 at 08:59
  • What is your intended behaviour? Is there a reason to create a new window for a PDF? The default behaviour will already open a new window. – amaitland May 07 '21 at 20:15

0 Answers0