Equalsk
Equalsk

Reputation: 8204

How can I get CefSharp to hide both the vertical and horizontal scrollbars when a webpage is loaded?

I'm using CefSharp 57 in a C# WPF application.
I want to hide both the vertical and horizontal scrollbars on any given webpage.

I've been unable to find a way to do this directly with CefSharp.
It doesn't seem to have a property similar to ScrollBarsEnabled that I'm used to with the native WinForms browser.

I thought I might be able to inject some CSS/JS after the page has rendered to set the body overflow to hidden but this has no effect.

private void OnBrowserFrameLoadEnd(object sender, FrameLoadEndEventArgs args)
{
    if (args.Frame.IsMain)
    {
        // Shows an alert after page is loaded so it definitely works
        args
            .Browser
            .MainFrame
            .ExecuteJavaScriptAsync("alert('HELLO!');

        // Scrollbars are still visible after this fires
        args
            .Browser
            .MainFrame
            .ExecuteJavaScriptAsync(
            "(function() { document.body.style.overflow = 'hidden'; });");
    }
}

I looked to see if there was a way to draw the scrollbars myself and make them transparent but I was unable to find a way to do this.

Is there a way to hide the vertical and horizontal scrollbars on any given page using CefSharp?

Upvotes: 4

Views: 9794

Answers (2)

Bessem Bousselmi
Bessem Bousselmi

Reputation: 351

You can add this parameter to hide scrollbars easy

  Brow = new CefSharp.WinForms.ChromiumWebBrowser("https://www.google.com") {
                Dock = DockStyle.Top,   // Hide scrollbars
            };

Upvotes: 0

Equalsk
Equalsk

Reputation: 8204

The code I originally showed is correct, I just needed to simplify the script I supplied:

WebBrowser.FrameLoadEnd += OnBrowserFrameLoadEnd;

private void OnBrowserFrameLoadEnd(object sender, FrameLoadEndEventArgs args)
{
    if (args.Frame.IsMain)
    {
        args
            .Browser
            .MainFrame
            .ExecuteJavaScriptAsync(
            "document.body.style.overflow = 'hidden'");
    }
}

The scrollbars are visible for just a second before disappearing which suits my purposes.

Upvotes: 14

Related Questions