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;
}