简体   繁体   中英

CefSharp 79.1.360 (Winform): How to reactivate popup in LifeSpanHandler

I have been able to handle the popup window when clicking a link in the main Cefsharp browser using the LifSpanHandler interface. However, if a different pop-up link is clicked, none of the events like OnBeforePopup or OnAfterCreated are fired in the LifSpanHandler interface. It works if I close the popup window. Are there an event that I can use to detect when the new link is loaded into the popup window when the popup window is already there? I put a break point in either events but neither is firing if the popup window is there.

My ultimate goal is to load a different window icon to the pop-up window depending on the link being clicked.

This is my LifeSpanHandler interface class:

using CefSharp;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;

namespace LifeSpanHandlerExample
{
    internal class LifeSpanHandler : ILifeSpanHandler
    {

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern bool SetWindowText(IntPtr hwnd, String lpString);

    [DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hwnd, int message, int wParam, IntPtr lParam);

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr SetFocus(IntPtr hWnd);

    [DllImport("user32.dll")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);

    private const int WM_SETICON = 0x80;
    private const int ICON_SMALL = 0;
    private const int ICON_BIG = 1;

    public bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
    {
        //
        return false; 
    }

    public void OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser)
    {
        if (!browser.IsDisposed && browser.IsPopup)
        {
            System.IntPtr _handle = browser.GetHost().GetWindowHandle();
            Bitmap bm = new Bitmap(Inc3DAppHelper.Properties.Resources.Incodema3D);
            Icon icon = Icon.FromHandle(bm.GetHicon());
            SendMessage(_handle, WM_SETICON, ICON_BIG, icon.Handle);
        }
    }

    public void OnBeforeClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
    {
        //
    }

    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)
    {
        //Default Behavior
        newBrowser = null;

        return false;  //return true to cancel popup creation
    }


}

}

Thank you to amaitland, I was able to implement the ILoadHandler as he suggested. This is pretty cool since it also solves the popup window not being able to popup to the front if it's already opened. I can also now change the icon by know the URL link but I'll work on that later. For other people trying to solve the same problem, here's the code for the LoadHandler interface class to bring the popup window up to the front.

using CefSharp;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;

namespace CefSharpExample
{
    internal class LoadHandler : ILoadHandler
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr SetFocus(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        public void OnFrameLoadEnd(IWebBrowser chromiumWebBrowser, FrameLoadEndEventArgs frameLoadEndArgs)
        {
          //
        }

        public void OnFrameLoadStart(IWebBrowser chromiumWebBrowser, FrameLoadStartEventArgs frameLoadStartArgs)
        {
        //
        }

        public void OnLoadError(IWebBrowser chromiumWebBrowser, LoadErrorEventArgs loadErrorArgs)
        {
             //
        }

        public void OnLoadingStateChange(IWebBrowser chromiumWebBrowser, LoadingStateChangedEventArgs loadingStateChangedArgs)
        {
             if (loadingStateChangedArgs.Browser.IsPopup)
             {
                System.IntPtr _handle = loadingStateChangedArgs.Browser.GetHost().GetWindowHandle();
                 {
                     SetFocus(_handle);
                     SetForegroundWindow(_handle);
                 }
             }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM