隐藏webBrowser滚动条wpf

时间:2013-03-26 08:58:27

标签: c# wpf browser scrollbar

我有一个webbrowser,我正在加载一个.html文件。问题是虽然我已经将ScrollViewer.VerticalScrollBarVisibility设置为“Hidden”,但滚动条仍然可见。

我也尝试过这种方法而且无法正常工作

<WebBrowser x:Name="personalizedWebBrowser" HorizontalAlignment="Left"  VerticalAlignment="Top" 
                ScrollViewer.CanContentScroll="False"
                ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
                ScrollViewer.VerticalScrollBarVisibility="Hidden"
                LoadCompleted="wb_LoadCompleted"/>


private void wb_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
     mshtml.IHTMLDocument2 dom = (mshtml.IHTMLDocument2)personalizedWebBrowser.Document;
     dom.body.style.overflow = "hidden";
}

你能否提出别的建议?

3 个答案:

答案 0 :(得分:2)

我通过在wpf项目中使用Windows窗体WebBrowser控件解决了这个问题:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        string curDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\help";

        System.Windows.Forms.Integration.WindowsFormsHost host =
            new System.Windows.Forms.Integration.WindowsFormsHost();
        System.Windows.Forms.WebBrowser webBrowser1 = new System.Windows.Forms.WebBrowser();
        host.Child = webBrowser1;

        webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;

        string sFileName = "file:///{0}/index.html";

        webBrowser1.Url = new Uri(String.Format(sFileName, curDir));
        webBrowser1.ScrollBarsEnabled = false;
        this.grid1.Children.Add(host);
    }

    private void webBrowser1_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
    {
        System.Windows.Forms.WebBrowser webBrowser1 = sender as System.Windows.Forms.WebBrowser;
        if(webBrowser1==null)return;
        webBrowser1.Document.Body.Style = "overflow:hidden";
    }

grid1用作webBrowser1的容器

我们还需要在项目中添加以下程序集引用: WindowsFormsIntegration,System.Windows.Forms

答案 1 :(得分:0)

将Microsoft.mshtml添加到项目引用中。您不需要更改xaml中的任何滚动属性,因为它们不是使用mshtml时控制WebBrowser的属性。在LoadCompleted函数中,您将更改Web浏览器的实际文档,如下所示:

private void webBrowserChat_LoadCompleted(object sender, NavigationEventArgs e)
{
    mshtml.IHTMLDocument2 documentText = (IHTMLDocument2)webBrowserChat.Document; 
    //this will access the document properties 
    documentText.body.parentElement.style.overflow = "hidden"; 
   // This will hide the scrollbar (Set to "auto" if you want to see when it passes the surfacelimit)
}

答案 2 :(得分:0)

对于使用 VS2019 的 vb.net,修改 DevDude 的解决方案如下:

Private Sub webObjectLoaded(ByVal sender as WebBrowser, Byval as NavigationEventArgs)
  Dim dom as MSHTML.IHTMLDocument2 = sender.Document
  dom.body.style.overflow = "hidden"
End Sub

我在堆栈面板中显示了多个浏览器,因此调用代码如下所示:

Dim wb = New WebBrowser()
wb.NavigateToString(txt)
AddHandler wb.LoadCompleted, AddressOf webObjectLoaded

我从 COM 添加了对“Microsoft HTML 对象库”的引用,它似乎在引用列表中显示为 Interop.MSHTML。