Friday, October 19, 2007 10:37 AMHello
I'm having problems obtaining the scroll position in an IE add-on, and then scrolling. But let's first focus on obtaining the position. I've tried the method I found that seemed to me it's the most recommended, namely via IHTMLDocument2::get_body, and IHTMLTextContainer::get_scrollTop . It doesn't work; more specifically, it works on some web pages such as
but doesn't work on others, including the Google and Yahoo main pages. I don't know, I may be doing something wrong but I don't think so, since sometimes it works. But it's a possibility of course. So here's how I obtain the IHTMLTextContainer pointer:
IHTMLTextContainer* GetIHTMLTextContainer (IHTMLDocument2* apDocument)
assert (apDocument != NULL);
IHTMLElement* htmlBody = NULL;
if (apDocument->get_body (&htmlBody) != S_OK)
if (htmlBody == NULL)
IHTMLTextContainer* pHTMLText = NULL;
IID_IHTMLTextContainer, (void **)&pHTMLText);
return pHTMLText;}then I use it like
get_scrollHeight always returns a non-0 integer I suppose is correct, but pos is always 0 on those pages that don't work, even if the scroll is not at 0. On the pages that work, the scroll pos is OK, of course.
So, I wonder if anybody has met this problem and knows how to deal with it. Please tell me if so. Also if you spot some error in my code sample.
Also, maybe there's another more reliable (actually, that is: very reliable) way of scrolling. I've searched for it but couldn't find it. I tried pretty hard to do it via windows functions, that is obtain a HWND for the control that has the scroll bar, then use Windows scroll functions. It didn't work, I believe I did obtain the control but couldn't get the scroll info (but I'm not sure, maybe it wasn't the right control). I imagine there should be a way, what the hell, it's just a control with a scroll bar, it shouldn't depend on the page that's underneath.
I maybe should mention that I have to do it in C++, outside .NET. It has to work in IE 6 + 7, on Win2000 to Vista.
Wednesday, November 28, 2007 5:28 AMThe answer to this problem is on
Don't call scrollTop on the document.body, but on document.documentElement.
Using mshtml and C# my code looks like this
WebBrowserClass wb = ...
HTMLDocument doc = wb.Document as HTMLDocument;