Search code examples
c#winformspopupcefsharpchromium-embedded

How can I detect that a Popup window is a PrintDialog in Cefsharp?


I want to know how I can detect that a Popup window is a PrintDialog in Cefsharp. Right now I have this condition in the if clause, but it doesn't detect that the Popup is a PrintDialog. I don't know if I am using the right function to catch the Dialog. Here is the code I am actually using:

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)
    {
        _logger.Debug($"[WebBrowser.ObjectBoundInJavascript] entered in the function successfully");
        this.popup_request?.Invoke(targetUrl);
        PrintDialog dialog = new PrintDialog();
        

        if (chromiumWebBrowser.GetFocusedFrame().GetType().Equals(dialog.GetType()))
        {
            _logger.Debug($"[WebBrowser.ObjectBoundInJavascript] It entered in the if successfully");
            //IFrame frame= chromiumWebBrowser1.GetFocusedFrame();
            //frame.ExecuteJavaScriptAsync("const event = new KeyboardEvent('keydown', 'Enter'); button.dispatchEvent(event);");
            SendKeys.Send("{ENTER}");
            _logger.Debug($"[WebBrowser.ObjectBoundInJavascript] ENTER sended successfully");
            newBrowser = null;
            return true;
        }
        _logger.Debug($"[WebBrowser.ObjectBoundInJavascript] after the if successfully");
        newBrowser = null;
        return false;
    }

Solution

  • i want to know how can i detect that a Popup window is a PrintDialog in Cefsharp

    The simple answer is you cannot. OnBeforePopup is only called before the browser creates a new window to host a popup.

    The Chromium Embedded Framework(CEF) for which CefSharp is wrapper doesn't provide any API for intercepting the native Chromium Print Dialog.

    See also https://github.com/cefsharp/CefSharp/wiki/General-Usage#printing