WebBrowser Control--Set scroll bar position

Source: Internet
Author: User
Tags assert tagname

An issue has been encountered: When you open a Web page with a WebBrowser control, the scroll bar in the Web page scrolls to the bottom.

Found a way online: Using DCOM component technology, get HTML interface pointers. (This technique is also used to do the JavaScript function in the call Web page earlier). The code is as follows:

Boolput_scrolltop ()

{

HRESULT hr;

CComPtr <idispatch > Spdisp;

IHTMLDocument2 *spdoc = NULL;

Spdisp = M_objwebbrowser.getdocument ();

hr = Spdisp->queryinterface (Iid_ihtmldocument2, (void**) &spdoc);

ASSERT (SUCCEEDED (HR));

ihtmlelement* pelement = NULL;

hr = Spdoc->get_body (&pelement);

ASSERT (SUCCEEDED (HR));

ihtmlelement2* pElement2 = NULL;

hr = Pelement->queryinterface (Iid_ihtmlelement2, (void**) &pelement2);

ASSERT (SUCCEEDED (HR));

Get the full height of scroll bars in a Web page

Long scrolltop;

Pelement2->get_scrollheight (&scrolltop);

Set the offset of the scroll bar to the top

Pelement2->put_scrolltop (scrolltop);

}

In the process of implementation, we found another problem, for a page with multiple frame, the above method is not good, so the following method is used:

Boolput_scrolltop (idispatch* pdisp)

{

HRESULT hr;

IHTMLDocument2 *spdoc = NULL;

ihtmlelementcollection* pcollection;

hr = Pdisp->queryinterface (Iid_ihtmldocument2, (void**) &spdoc);

if (FAILED (HR))

{

return false;

}

hr = Spdoc->get_all (&pcollection);

if (FAILED (HR))

{

return false;

}

Long Len;

hr = Pcollection->get_length (&len);

if (FAILED (HR))

{

return false;

}

Iterate through all the elements

For (Long l=0 l <len; l++)

{

VARIANT VarIndex, var2;

VariantInit (&varindex);

VariantInit (&VAR2);

VARINDEX.VT = VT_I4;

Varindex.lval = l;

idispatch* spdisp = NULL;

Pcollection->item (VarIndex, var2, &spdisp);

ihtmlelement* Pelem;

Spdisp->queryinterface (Iid_ihtmlelement, (lpvoid*) &pelem);

CComBSTR TagName;

Pelem->get_tagname (&tagname);

CString str = tagName;

Str. Makeupper ();

if (Str.compare (_t ("IFRAME")) = = 0 | | Str.compare (_t ("FRAME")) = = 0)

{

ihtmlelement2* pElement2 = NULL;

hr = Pelem->queryinterface (Iid_ihtmlelement2, (void**) &pelement2);

if (FAILED (HR))

{

return false;

}

Long lscrollheight;

hr = Pelement2->get_scrollheight (&lscrollheight);

if (FAILED (HR))

{

return false;

}

hr = Pelement2->put_scrolltop (lscrollheight);

if (FAILED (HR))

{

return false;

}

Pelement2->release ();

}

Pelem->release ();

Spdisp->release ();

}

Pcollection->release ();

Spdoc->release ();

return true;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.