|
|
|
date: Fri, 19 Oct 2007 03:05:19 -0700,
group: microsoft.public.inetsdk.programming.webbrowser_ctl
back
scrolling in an IE add-on
Hello
I know this question has been asked before in this group. 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 right here and 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
http://news.google.com/nwshp?tab=wn
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)
return NULL;
if (htmlBody == NULL)
return NULL;
IHTMLTextContainer* pHTMLText = NULL;
htmlBody->QueryInterface (
IID_IHTMLTextContainer, (void **)&pHTMLText);
htmlBody->Release();
return pHTMLText;
}
then I use it like
pHTMLText->get_scrollTop (&pos);
pHTMLText->get_scrollHeight (&range);
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.
Regards
V. Simionescu
date: Fri, 19 Oct 2007 03:05:19 -0700
author: unknown
Re: scrolling in an IE add-on
wrote in message
news:1192788319.534623.174650@v23g2000prn.googlegroups.com
> I know this question has been asked before in this group. 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 right here and 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
>
> http://news.google.com/nwshp?tab=wn
>
> but doesn't work on others, including the Google and Yahoo main pages.
http://groups.google.com/group/microsoft.public.inetsdk.programming.webbrowser_ctl/browse_frm/thread/fa3452e1bbb6bcf5
> Also, maybe there's another more reliable (actually, that is: very
> reliable) way of scrolling.
put_scrollTop
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
date: Fri, 19 Oct 2007 08:04:34 -0400
author: Igor Tandetnik
Re: scrolling in an IE add-on
wrote in message
news:1192797930.903193.127400@i13g2000prf.googlegroups.com
> Igor Tandetnik wrote:
>
>> put_scrollTop
>
> What put_scrollTop ?
A method. scrollTop is a read/write property. Just as you can read it to
find out how far the page is scrolled, you can assign to it to make the
page scroll. Didn't you ask for a way to do that?
> Excuse me, but have you read what I've asked ?
Yes I have. Have you read what I answered?
> I'm trying to obtain the scroll position, I do it with get_scrollTop,
> on some pages it works, on others it doesn't.
I assume you have followed the link I provided, read the thread thereat,
and found that it didn't completely answer your question. What specific
deficiencies did you discover?
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
date: Fri, 19 Oct 2007 09:03:59 -0400
author: Igor Tandetnik
Re: scrolling in an IE add-on
Igor Tandetnik wrote:
> wrote in message
> news:1192797930.903193.127400@i13g2000prf.googlegroups.com
> > Igor Tandetnik wrote:
> >
> >> put_scrollTop
> >
> > What put_scrollTop ?
>
> A method. scrollTop is a read/write property. Just as you can read it to
> find out how far the page is scrolled, you can assign to it to make the
> page scroll. Didn't you ask for a way to do that?
>
> > Excuse me, but have you read what I've asked ?
>
> Yes I have. Have you read what I answered?
>
> > I'm trying to obtain the scroll position, I do it with get_scrollTop,
> > on some pages it works, on others it doesn't.
>
> I assume you have followed the link I provided, read the thread thereat,
> and found that it didn't completely answer your question. What specific
> deficiencies did you discover?
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925
OK, sorry. I hadn't noticed your link at all. Don't take this the
wrong way but even now after you've made me pay attention, I don't
think it was entirely my fault. You should have said something like
"see this discussion for an explanation", like it is it just appeared
like a link from my own message.
Anyway, thanks a lot. I haven't tested it yet but it surely is an
answer to my question. So this is what I'm going to do right now, and
I'll get back here if there are problems left. If I don't it means
everything went fine. Thanks again.
V. Sim.
date: Sat, 20 Oct 2007 23:45:08 -0700
author: unknown
Re: scrolling in an IE add-on
wrote in message
news:1192963319.011991.109320@v29g2000prd.googlegroups.com
> One more simple question. I'm having trouble accessing the
> IHTMLDocument3 interface in VC6.
Download and install February 2003 Platform SDK (the most recent SDK
that still works with VC6) here:
http://www.microsoft.com/msdownload/platformsdk/sdkupdate/psdk-full.htm
> I know I have to install the platform
> SDK etc., I've tried and it didn't work easily so it looks like work.
While it's fun and games most of the time, programmers are known to have
to do work occasionally. It's an occupational hazard.
> I'll do it if I have to but I believe it should work much easier by
> obtaining the document element via body.parentElement.
Yes, this should work. I don't quite see why it is better or easier, but
if you feel so, by all means go for it.
> Do you see any
> problem with this ? I don't see why it wouldn't work. Except that it
> doesn't. I do get the parent element, I checked and it has the tag
> name HTML, so everything seems to be OK. Except that when I query it
> for the IHTMLTextContainer, it returns NULL.
Try querying for IHTMLElement2 instead.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
date: Mon, 22 Oct 2007 08:16:14 -0400
author: Igor Tandetnik
Re: scrolling in an IE add-on
Igor Tandetnik wrote:
> wrote in message
> news:1192963319.011991.109320@v29g2000prd.googlegroups.com
> > One more simple question. I'm having trouble accessing the
> > IHTMLDocument3 interface in VC6.
>
> Download and install February 2003 Platform SDK (the most recent SDK
> that still works with VC6) here:
>
> http://www.microsoft.com/msdownload/platformsdk/sdkupdate/psdk-full.htm
Thanks again. You do know everything, don't you ?
> While it's fun and games most of the time, programmers are known to have
> to do work occasionally. It's an occupational hazard.
>
Yes, but it's sound practice to avoid this as much as possible.
> > I'll do it if I have to but I believe it should work much easier by
> > obtaining the document element via body.parentElement.
>
> Yes, this should work. I don't quite see why it is better or easier, but
> if you feel so, by all means go for it.
>
Simple, because I thought I'd avoid installing the platform SDK.
Unfortunately IHTMLElement2 also cannot be accessed with the standard
VC6 include files, so it looks like I'll have to do some work.
OK, so I'll do the work and get back with the results. Thanks.
V. Sim.
date: Mon, 22 Oct 2007 06:19:28 -0700
author: unknown
Re: scrolling in an IE add-on
Hurray! Looks like it's finally working OK on every page I could test.
Hopefully on all.
I have to say, this is one of the best pieces of programming advice
I've ever received on an Internet discussion list.
One remark though, I've done a little testing and I've found that
scrollWidth and scrollHeight should always be retrieved from the body
element, even for the pages that scroll via the HTML element. As if it
wasn't already way too complicated. For HTML-scrollable pages, from
what I've seen the body and HTML elements return the same thing for
these 2 properties. For body-scrollable pages, the body returns the
right value, while the HTML element seems to return the width and
height of the document area. Anyway, these shouldn't be added, like
the top and left properties, but always just take the value returned
by the body element.
To summarise scrolling the document area in an IE add-on:
- use the scrollLeft, scrollTop, scrollWidth and scrollHeight
properties of either the body or the HTML (document) element, as
detailed below; see interface IHTMLElement2 in MSDN for their meaning
and how to use them
- Some pages are scrollable via the body element, others via the HTML
element. For the body-scrollable ones, scrollLeft and scrollTop of the
HTML element are always 0, and vice-versa. So, to obtain the real
scrollLeft and scrollTop in every case, get them from both elements
and add them. To change them, set them into both elements; the one
that's not scrollable will ignore this.
- width and height are best obtained always from the body element,
regardless which one is the scrollable element
- all these properties can be accessed by the IHTMLElement2 interface
I guess that's all.
I hope nobody will mind if I share here my 2 functions for doing all
this (plus one auxiliary function). Actually, I hope I might even be
of some help to some with them. Feel free to use them for any purpose,
but of course at your own risk, I've just tested them but they might
still contain a few bugs.
void GetHTMLScrollPointers (IHTMLDocument2* apDocument,
IHTMLElement2** apHTMLElement2, IHTMLElement2** apBodyElement2)
{
assert (apDocument != NULL);
assert (apHTMLElement2 != NULL);
assert (apBodyElement2 != NULL);
assert (*apHTMLElement2 == NULL);
assert (*apBodyElement2 == NULL);
if (*apHTMLElement2 != NULL || *apBodyElement2 != NULL)
// possible in release mode
return; // don't accept this
IHTMLElement* htmlBody = NULL;
apDocument->get_body (&htmlBody);
if (htmlBody == NULL)
return;
htmlBody->QueryInterface (IID_IHTMLElement2, (void
**)apBodyElement2);
// looks like *apBodyElement2 is always non-NULL if it's OK,
otherwise NULL
IHTMLElement* html = NULL;
htmlBody->get_parentElement (&html);
htmlBody->Release();
if (html == NULL) // should never happen actually
return;
html->QueryInterface (IID_IHTMLElement2, (void **)apHTMLElement2);
// looks like *apHTMLElement2 is always non-NULL if it's OK,
otherwise NULL
html->Release();
}
// any of the 4 return pointers may be NULL, meaning this info is not
needed
void GetScrollState (IHTMLDocument2* apDocument,
long* apLeft, long* apTop, long* apWidth, long* apHeight)
{
assert (apDocument != NULL);
if (apLeft != NULL)
*apLeft = 0;
if (apTop != NULL)
*apTop = 0;
if (apWidth != NULL)
*apWidth = 0;
if (apHeight != NULL)
*apHeight = 0;
IHTMLElement2* pHTMLElement2 = NULL, * pBodyElement2 = NULL;
GetHTMLScrollPointers (apDocument, &pHTMLElement2,
&pBodyElement2);
long leftBody = 0, topBody = 0, widthBody = 0, heightBody = 0;
if (pBodyElement2 != NULL)
{
pBodyElement2->get_scrollLeft (&leftBody);
pBodyElement2->get_scrollTop (&topBody);
pBodyElement2->get_scrollWidth (&widthBody);
pBodyElement2->get_scrollHeight (&heightBody);
pBodyElement2->Release ();
}
long leftHTML = 0, topHTML = 0, widthHTML = 0, heightHTML = 0;
if (pHTMLElement2 != NULL)
{
pHTMLElement2->get_scrollLeft (&leftHTML);
pHTMLElement2->get_scrollTop (&topHTML);
pHTMLElement2->get_scrollWidth (&widthHTML);
pHTMLElement2->get_scrollHeight (&heightHTML);
pHTMLElement2->Release ();
}
if (apLeft != NULL)
*apLeft = leftBody + leftHTML;
if (apTop != NULL)
*apTop = topBody + topHTML;
// take width and height from body elem. always
if (apWidth != NULL)
*apWidth = widthBody;
if (apHeight != NULL)
*apHeight = heightBody;
}
// set aLeft, aTop to -1 if you don't want them changed
void Scroll (IHTMLDocument2* apDocument, long aLeft, long aTop)
{
assert (apDocument != NULL);
IHTMLElement2* pHTMLElement2 = NULL, * pBodyElement2 = NULL;
GetHTMLScrollPointers (apDocument, &pHTMLElement2,
&pBodyElement2);
if (pBodyElement2 != NULL)
{
if (aLeft >= 0)
pBodyElement2->put_scrollLeft (aLeft);
if (aTop >= 0)
pBodyElement2->put_scrollTop (aTop);
pBodyElement2->Release ();
}
if (pHTMLElement2 != NULL)
{
if (aLeft >= 0)
pHTMLElement2->put_scrollLeft (aLeft);
if (aTop >= 0)
pHTMLElement2->put_scrollTop (aTop);
pHTMLElement2->Release ();
}
}
date: Wed, 24 Oct 2007 11:05:49 -0700
author: unknown
|
|