views:

617

answers:

2

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?

+3  A: 

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

serge_gubenko
+1. Sounds right :)
Anvaka
Unfortunately WebBrowser.Document never seems to be set. This may be a side-effect of how I load pages, I'm not sure.
Kevin Montrose
can you post up some code of how are you loading your pages. thnx
serge_gubenko
`WebBrowser.NavigateToString`, with all this document tomfoolery occurring in the `Loaded` event.
Kevin Montrose
I guess as you already know there is nothing special in NavigateToString comparing to other ways of loading webbrowser's content; you would want to check the LoadCompleted event (http://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser.loadcompleted.aspx) and put your rescaling code there; at this moment document object should be completely initialized
serge_gubenko
Silly me, confusing `Loaded` and `LoadCompleted`.
Kevin Montrose
+1  A: 

Hi,

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

Assaf S.