Prevent scrollbars with WPF WebBrowser displaying content

旧巷老猫 提交于 2020-01-21 03:02:26

问题


I'm using the WPF WebBrowser component to display some very simple HTML content. However, since I don't know the content size in advance, I'm currently getting scrollbars on the control when I load certain datasets.

Basically, how can I force (or otherwise effect the equivalent of forcing) the WebBrowser to expand in size so that all content is displayed without the need for scrollbars?


回答1:


I guess you can get width and height of the webbrowser component content through its Document property which should be of mshtml.HTMLDocument type. I believe you should be able to use body or documentElement properties to get needed sizes; smth like this:

mshtml.HTMLDocument htmlDoc = webBrowser.Document as mshtml.HTMLDocument;
if (htmlDoc != null && htmlDoc.body != null)
{
    mshtml.IHTMLElement2 body = (mshtml.IHTMLElement2)htmlDoc.body;
    webBrowser.Width = body.scrollWidth;
    webBrowser.Height = body.scrollHeight;
}

hope this helps, regards




回答2:


The problem with previous solution is that it changes the control size and since the browser control cannot be clipped and is always on top of other WPF elements, it may cover the other elements.

Here is my solution:

Dim body = CType(WebBrowserControl.Document, mshtml.HTMLDocumentClass)
body.documentElement.style.overflow = "hidden"

Regards,

Assaf



来源:https://stackoverflow.com/questions/1911578/prevent-scrollbars-with-wpf-webbrowser-displaying-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!